mvebu-mbus.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. struct resource pcie_mem_aperture;
  133. struct resource pcie_io_aperture;
  134. const struct mvebu_mbus_soc_data *soc;
  135. int hw_io_coherency;
  136. };
  137. static struct mvebu_mbus_state mbus_state;
  138. static struct mbus_dram_target_info mvebu_mbus_dram_info;
  139. const struct mbus_dram_target_info *mv_mbus_dram_info(void)
  140. {
  141. return &mvebu_mbus_dram_info;
  142. }
  143. EXPORT_SYMBOL_GPL(mv_mbus_dram_info);
  144. /*
  145. * Functions to manipulate the address decoding windows
  146. */
  147. static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
  148. int win, int *enabled, u64 *base,
  149. u32 *size, u8 *target, u8 *attr,
  150. u64 *remap)
  151. {
  152. void __iomem *addr = mbus->mbuswins_base +
  153. mbus->soc->win_cfg_offset(win);
  154. u32 basereg = readl(addr + WIN_BASE_OFF);
  155. u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
  156. if (!(ctrlreg & WIN_CTRL_ENABLE)) {
  157. *enabled = 0;
  158. return;
  159. }
  160. *enabled = 1;
  161. *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
  162. *base |= (basereg & WIN_BASE_LOW);
  163. *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
  164. if (target)
  165. *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
  166. if (attr)
  167. *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
  168. if (remap) {
  169. if (win < mbus->soc->num_remappable_wins) {
  170. u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
  171. u32 remap_hi = readl(addr + WIN_REMAP_HI_OFF);
  172. *remap = ((u64)remap_hi << 32) | remap_low;
  173. } else
  174. *remap = 0;
  175. }
  176. }
  177. static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
  178. int win)
  179. {
  180. void __iomem *addr;
  181. addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
  182. writel(0, addr + WIN_BASE_OFF);
  183. writel(0, addr + WIN_CTRL_OFF);
  184. if (win < mbus->soc->num_remappable_wins) {
  185. writel(0, addr + WIN_REMAP_LO_OFF);
  186. writel(0, addr + WIN_REMAP_HI_OFF);
  187. }
  188. }
  189. /* Checks whether the given window number is available */
  190. static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
  191. const int win)
  192. {
  193. void __iomem *addr = mbus->mbuswins_base +
  194. mbus->soc->win_cfg_offset(win);
  195. u32 ctrl = readl(addr + WIN_CTRL_OFF);
  196. return !(ctrl & WIN_CTRL_ENABLE);
  197. }
  198. /*
  199. * Checks whether the given (base, base+size) area doesn't overlap an
  200. * existing region
  201. */
  202. static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
  203. phys_addr_t base, size_t size,
  204. u8 target, u8 attr)
  205. {
  206. u64 end = (u64)base + size;
  207. int win;
  208. for (win = 0; win < mbus->soc->num_wins; win++) {
  209. u64 wbase, wend;
  210. u32 wsize;
  211. u8 wtarget, wattr;
  212. int enabled;
  213. mvebu_mbus_read_window(mbus, win,
  214. &enabled, &wbase, &wsize,
  215. &wtarget, &wattr, NULL);
  216. if (!enabled)
  217. continue;
  218. wend = wbase + wsize;
  219. /*
  220. * Check if the current window overlaps with the
  221. * proposed physical range
  222. */
  223. if ((u64)base < wend && end > wbase)
  224. return 0;
  225. /*
  226. * Check if target/attribute conflicts
  227. */
  228. if (target == wtarget && attr == wattr)
  229. return 0;
  230. }
  231. return 1;
  232. }
  233. static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
  234. phys_addr_t base, size_t size)
  235. {
  236. int win;
  237. for (win = 0; win < mbus->soc->num_wins; win++) {
  238. u64 wbase;
  239. u32 wsize;
  240. int enabled;
  241. mvebu_mbus_read_window(mbus, win,
  242. &enabled, &wbase, &wsize,
  243. NULL, NULL, NULL);
  244. if (!enabled)
  245. continue;
  246. if (base == wbase && size == wsize)
  247. return win;
  248. }
  249. return -ENODEV;
  250. }
  251. static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
  252. int win, phys_addr_t base, size_t size,
  253. phys_addr_t remap, u8 target,
  254. u8 attr)
  255. {
  256. void __iomem *addr = mbus->mbuswins_base +
  257. mbus->soc->win_cfg_offset(win);
  258. u32 ctrl, remap_addr;
  259. ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
  260. (attr << WIN_CTRL_ATTR_SHIFT) |
  261. (target << WIN_CTRL_TGT_SHIFT) |
  262. WIN_CTRL_ENABLE;
  263. writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
  264. writel(ctrl, addr + WIN_CTRL_OFF);
  265. if (win < mbus->soc->num_remappable_wins) {
  266. if (remap == MVEBU_MBUS_NO_REMAP)
  267. remap_addr = base;
  268. else
  269. remap_addr = remap;
  270. writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
  271. writel(0, addr + WIN_REMAP_HI_OFF);
  272. }
  273. return 0;
  274. }
  275. static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
  276. phys_addr_t base, size_t size,
  277. phys_addr_t remap, u8 target,
  278. u8 attr)
  279. {
  280. int win;
  281. if (remap == MVEBU_MBUS_NO_REMAP) {
  282. for (win = mbus->soc->num_remappable_wins;
  283. win < mbus->soc->num_wins; win++)
  284. if (mvebu_mbus_window_is_free(mbus, win))
  285. return mvebu_mbus_setup_window(mbus, win, base,
  286. size, remap,
  287. target, attr);
  288. }
  289. for (win = 0; win < mbus->soc->num_wins; win++)
  290. if (mvebu_mbus_window_is_free(mbus, win))
  291. return mvebu_mbus_setup_window(mbus, win, base, size,
  292. remap, target, attr);
  293. return -ENOMEM;
  294. }
  295. /*
  296. * Debugfs debugging
  297. */
  298. /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
  299. static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
  300. struct seq_file *seq, void *v)
  301. {
  302. int i;
  303. for (i = 0; i < 4; i++) {
  304. u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  305. u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  306. u64 base;
  307. u32 size;
  308. if (!(sizereg & DDR_SIZE_ENABLED)) {
  309. seq_printf(seq, "[%d] disabled\n", i);
  310. continue;
  311. }
  312. base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
  313. base |= basereg & DDR_BASE_CS_LOW_MASK;
  314. size = (sizereg | ~DDR_SIZE_MASK);
  315. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  316. i, (unsigned long long)base,
  317. (unsigned long long)base + size + 1,
  318. (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
  319. }
  320. return 0;
  321. }
  322. /* Special function for Dove */
  323. static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
  324. struct seq_file *seq, void *v)
  325. {
  326. int i;
  327. for (i = 0; i < 2; i++) {
  328. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  329. u64 base;
  330. u32 size;
  331. if (!(map & 1)) {
  332. seq_printf(seq, "[%d] disabled\n", i);
  333. continue;
  334. }
  335. base = map & 0xff800000;
  336. size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  337. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  338. i, (unsigned long long)base,
  339. (unsigned long long)base + size, i);
  340. }
  341. return 0;
  342. }
  343. static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
  344. {
  345. struct mvebu_mbus_state *mbus = &mbus_state;
  346. return mbus->soc->show_cpu_target(mbus, seq, v);
  347. }
  348. static int mvebu_sdram_debug_open(struct inode *inode, struct file *file)
  349. {
  350. return single_open(file, mvebu_sdram_debug_show, inode->i_private);
  351. }
  352. static const struct file_operations mvebu_sdram_debug_fops = {
  353. .open = mvebu_sdram_debug_open,
  354. .read = seq_read,
  355. .llseek = seq_lseek,
  356. .release = single_release,
  357. };
  358. static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
  359. {
  360. struct mvebu_mbus_state *mbus = &mbus_state;
  361. int win;
  362. for (win = 0; win < mbus->soc->num_wins; win++) {
  363. u64 wbase, wremap;
  364. u32 wsize;
  365. u8 wtarget, wattr;
  366. int enabled, i;
  367. const char *name;
  368. mvebu_mbus_read_window(mbus, win,
  369. &enabled, &wbase, &wsize,
  370. &wtarget, &wattr, &wremap);
  371. if (!enabled) {
  372. seq_printf(seq, "[%02d] disabled\n", win);
  373. continue;
  374. }
  375. for (i = 0; mbus->soc->map[i].name; i++)
  376. if (mbus->soc->map[i].target == wtarget &&
  377. mbus->soc->map[i].attr ==
  378. (wattr & mbus->soc->map[i].attrmask))
  379. break;
  380. name = mbus->soc->map[i].name ?: "unknown";
  381. seq_printf(seq, "[%02d] %016llx - %016llx : %s",
  382. win, (unsigned long long)wbase,
  383. (unsigned long long)(wbase + wsize), name);
  384. if (win < mbus->soc->num_remappable_wins) {
  385. seq_printf(seq, " (remap %016llx)\n",
  386. (unsigned long long)wremap);
  387. } else
  388. seq_printf(seq, "\n");
  389. }
  390. return 0;
  391. }
  392. static int mvebu_devs_debug_open(struct inode *inode, struct file *file)
  393. {
  394. return single_open(file, mvebu_devs_debug_show, inode->i_private);
  395. }
  396. static const struct file_operations mvebu_devs_debug_fops = {
  397. .open = mvebu_devs_debug_open,
  398. .read = seq_read,
  399. .llseek = seq_lseek,
  400. .release = single_release,
  401. };
  402. /*
  403. * SoC-specific functions and definitions
  404. */
  405. static unsigned int orion_mbus_win_offset(int win)
  406. {
  407. return win << 4;
  408. }
  409. static unsigned int armada_370_xp_mbus_win_offset(int win)
  410. {
  411. /* The register layout is a bit annoying and the below code
  412. * tries to cope with it.
  413. * - At offset 0x0, there are the registers for the first 8
  414. * windows, with 4 registers of 32 bits per window (ctrl,
  415. * base, remap low, remap high)
  416. * - Then at offset 0x80, there is a hole of 0x10 bytes for
  417. * the internal registers base address and internal units
  418. * sync barrier register.
  419. * - Then at offset 0x90, there the registers for 12
  420. * windows, with only 2 registers of 32 bits per window
  421. * (ctrl, base).
  422. */
  423. if (win < 8)
  424. return win << 4;
  425. else
  426. return 0x90 + ((win - 8) << 3);
  427. }
  428. static unsigned int mv78xx0_mbus_win_offset(int win)
  429. {
  430. if (win < 8)
  431. return win << 4;
  432. else
  433. return 0x900 + ((win - 8) << 4);
  434. }
  435. static void __init
  436. mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
  437. {
  438. int i;
  439. int cs;
  440. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  441. for (i = 0, cs = 0; i < 4; i++) {
  442. u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  443. u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  444. /*
  445. * We only take care of entries for which the chip
  446. * select is enabled, and that don't have high base
  447. * address bits set (devices can only access the first
  448. * 32 bits of the memory).
  449. */
  450. if ((size & DDR_SIZE_ENABLED) &&
  451. !(base & DDR_BASE_CS_HIGH_MASK)) {
  452. struct mbus_dram_window *w;
  453. w = &mvebu_mbus_dram_info.cs[cs++];
  454. w->cs_index = i;
  455. w->mbus_attr = 0xf & ~(1 << i);
  456. if (mbus->hw_io_coherency)
  457. w->mbus_attr |= ATTR_HW_COHERENCY;
  458. w->base = base & DDR_BASE_CS_LOW_MASK;
  459. w->size = (size | ~DDR_SIZE_MASK) + 1;
  460. }
  461. }
  462. mvebu_mbus_dram_info.num_cs = cs;
  463. }
  464. static void __init
  465. mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
  466. {
  467. int i;
  468. int cs;
  469. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  470. for (i = 0, cs = 0; i < 2; i++) {
  471. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  472. /*
  473. * Chip select enabled?
  474. */
  475. if (map & 1) {
  476. struct mbus_dram_window *w;
  477. w = &mvebu_mbus_dram_info.cs[cs++];
  478. w->cs_index = i;
  479. w->mbus_attr = 0; /* CS address decoding done inside */
  480. /* the DDR controller, no need to */
  481. /* provide attributes */
  482. w->base = map & 0xff800000;
  483. w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  484. }
  485. }
  486. mvebu_mbus_dram_info.num_cs = cs;
  487. }
  488. static const struct mvebu_mbus_mapping armada_370_map[] = {
  489. MAPDEF("bootrom", 1, 0xe0, MAPDEF_NOMASK),
  490. MAPDEF("devbus-boot", 1, 0x2f, MAPDEF_NOMASK),
  491. MAPDEF("devbus-cs0", 1, 0x3e, MAPDEF_NOMASK),
  492. MAPDEF("devbus-cs1", 1, 0x3d, MAPDEF_NOMASK),
  493. MAPDEF("devbus-cs2", 1, 0x3b, MAPDEF_NOMASK),
  494. MAPDEF("devbus-cs3", 1, 0x37, MAPDEF_NOMASK),
  495. MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
  496. MAPDEF("pcie1.0", 8, 0xe0, MAPDEF_PCIMASK),
  497. {},
  498. };
  499. static const struct mvebu_mbus_soc_data armada_370_mbus_data = {
  500. .num_wins = 20,
  501. .num_remappable_wins = 8,
  502. .win_cfg_offset = armada_370_xp_mbus_win_offset,
  503. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  504. .show_cpu_target = mvebu_sdram_debug_show_orion,
  505. .map = armada_370_map,
  506. };
  507. static const struct mvebu_mbus_mapping armada_xp_map[] = {
  508. MAPDEF("bootrom", 1, 0x1d, MAPDEF_NOMASK),
  509. MAPDEF("devbus-boot", 1, 0x2f, MAPDEF_NOMASK),
  510. MAPDEF("devbus-cs0", 1, 0x3e, MAPDEF_NOMASK),
  511. MAPDEF("devbus-cs1", 1, 0x3d, MAPDEF_NOMASK),
  512. MAPDEF("devbus-cs2", 1, 0x3b, MAPDEF_NOMASK),
  513. MAPDEF("devbus-cs3", 1, 0x37, MAPDEF_NOMASK),
  514. MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
  515. MAPDEF("pcie0.1", 4, 0xd0, MAPDEF_PCIMASK),
  516. MAPDEF("pcie0.2", 4, 0xb0, MAPDEF_PCIMASK),
  517. MAPDEF("pcie0.3", 4, 0x70, MAPDEF_PCIMASK),
  518. MAPDEF("pcie1.0", 8, 0xe0, MAPDEF_PCIMASK),
  519. MAPDEF("pcie1.1", 8, 0xd0, MAPDEF_PCIMASK),
  520. MAPDEF("pcie1.2", 8, 0xb0, MAPDEF_PCIMASK),
  521. MAPDEF("pcie1.3", 8, 0x70, MAPDEF_PCIMASK),
  522. MAPDEF("pcie2.0", 4, 0xf0, MAPDEF_PCIMASK),
  523. MAPDEF("pcie3.0", 8, 0xf0, MAPDEF_PCIMASK),
  524. {},
  525. };
  526. static const struct mvebu_mbus_soc_data armada_xp_mbus_data = {
  527. .num_wins = 20,
  528. .num_remappable_wins = 8,
  529. .win_cfg_offset = armada_370_xp_mbus_win_offset,
  530. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  531. .show_cpu_target = mvebu_sdram_debug_show_orion,
  532. .map = armada_xp_map,
  533. };
  534. static const struct mvebu_mbus_mapping kirkwood_map[] = {
  535. MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
  536. MAPDEF("pcie1.0", 4, 0xd0, MAPDEF_PCIMASK),
  537. MAPDEF("sram", 3, 0x01, MAPDEF_NOMASK),
  538. MAPDEF("nand", 1, 0x2f, MAPDEF_NOMASK),
  539. {},
  540. };
  541. static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
  542. .num_wins = 8,
  543. .num_remappable_wins = 4,
  544. .win_cfg_offset = orion_mbus_win_offset,
  545. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  546. .show_cpu_target = mvebu_sdram_debug_show_orion,
  547. .map = kirkwood_map,
  548. };
  549. static const struct mvebu_mbus_mapping dove_map[] = {
  550. MAPDEF("pcie0.0", 0x4, 0xe0, MAPDEF_PCIMASK),
  551. MAPDEF("pcie1.0", 0x8, 0xe0, MAPDEF_PCIMASK),
  552. MAPDEF("cesa", 0x3, 0x01, MAPDEF_NOMASK),
  553. MAPDEF("bootrom", 0x1, 0xfd, MAPDEF_NOMASK),
  554. MAPDEF("scratchpad", 0xd, 0x0, MAPDEF_NOMASK),
  555. {},
  556. };
  557. static const struct mvebu_mbus_soc_data dove_mbus_data = {
  558. .num_wins = 8,
  559. .num_remappable_wins = 4,
  560. .win_cfg_offset = orion_mbus_win_offset,
  561. .setup_cpu_target = mvebu_mbus_dove_setup_cpu_target,
  562. .show_cpu_target = mvebu_sdram_debug_show_dove,
  563. .map = dove_map,
  564. };
  565. static const struct mvebu_mbus_mapping orion5x_map[] = {
  566. MAPDEF("pcie0.0", 4, 0x51, MAPDEF_ORIONPCIMASK),
  567. MAPDEF("pci0.0", 3, 0x51, MAPDEF_ORIONPCIMASK),
  568. MAPDEF("devbus-boot", 1, 0x0f, MAPDEF_NOMASK),
  569. MAPDEF("devbus-cs0", 1, 0x1e, MAPDEF_NOMASK),
  570. MAPDEF("devbus-cs1", 1, 0x1d, MAPDEF_NOMASK),
  571. MAPDEF("devbus-cs2", 1, 0x1b, MAPDEF_NOMASK),
  572. MAPDEF("sram", 0, 0x00, MAPDEF_NOMASK),
  573. {},
  574. };
  575. /*
  576. * Some variants of Orion5x have 4 remappable windows, some other have
  577. * only two of them.
  578. */
  579. static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
  580. .num_wins = 8,
  581. .num_remappable_wins = 4,
  582. .win_cfg_offset = orion_mbus_win_offset,
  583. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  584. .show_cpu_target = mvebu_sdram_debug_show_orion,
  585. .map = orion5x_map,
  586. };
  587. static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
  588. .num_wins = 8,
  589. .num_remappable_wins = 2,
  590. .win_cfg_offset = orion_mbus_win_offset,
  591. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  592. .show_cpu_target = mvebu_sdram_debug_show_orion,
  593. .map = orion5x_map,
  594. };
  595. static const struct mvebu_mbus_mapping mv78xx0_map[] = {
  596. MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
  597. MAPDEF("pcie0.1", 4, 0xd0, MAPDEF_PCIMASK),
  598. MAPDEF("pcie0.2", 4, 0xb0, MAPDEF_PCIMASK),
  599. MAPDEF("pcie0.3", 4, 0x70, MAPDEF_PCIMASK),
  600. MAPDEF("pcie1.0", 8, 0xe0, MAPDEF_PCIMASK),
  601. MAPDEF("pcie1.1", 8, 0xd0, MAPDEF_PCIMASK),
  602. MAPDEF("pcie1.2", 8, 0xb0, MAPDEF_PCIMASK),
  603. MAPDEF("pcie1.3", 8, 0x70, MAPDEF_PCIMASK),
  604. MAPDEF("pcie2.0", 4, 0xf0, MAPDEF_PCIMASK),
  605. MAPDEF("pcie3.0", 8, 0xf0, MAPDEF_PCIMASK),
  606. {},
  607. };
  608. static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
  609. .num_wins = 14,
  610. .num_remappable_wins = 8,
  611. .win_cfg_offset = mv78xx0_mbus_win_offset,
  612. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  613. .show_cpu_target = mvebu_sdram_debug_show_orion,
  614. .map = mv78xx0_map,
  615. };
  616. /*
  617. * The driver doesn't yet have a DT binding because the details of
  618. * this DT binding still need to be sorted out. However, as a
  619. * preparation, we already use of_device_id to match a SoC description
  620. * string against the SoC specific details of this driver.
  621. */
  622. static const struct of_device_id of_mvebu_mbus_ids[] = {
  623. { .compatible = "marvell,armada370-mbus",
  624. .data = &armada_370_mbus_data, },
  625. { .compatible = "marvell,armadaxp-mbus",
  626. .data = &armada_xp_mbus_data, },
  627. { .compatible = "marvell,kirkwood-mbus",
  628. .data = &kirkwood_mbus_data, },
  629. { .compatible = "marvell,dove-mbus",
  630. .data = &dove_mbus_data, },
  631. { .compatible = "marvell,orion5x-88f5281-mbus",
  632. .data = &orion5x_4win_mbus_data, },
  633. { .compatible = "marvell,orion5x-88f5182-mbus",
  634. .data = &orion5x_2win_mbus_data, },
  635. { .compatible = "marvell,orion5x-88f5181-mbus",
  636. .data = &orion5x_2win_mbus_data, },
  637. { .compatible = "marvell,orion5x-88f6183-mbus",
  638. .data = &orion5x_4win_mbus_data, },
  639. { .compatible = "marvell,mv78xx0-mbus",
  640. .data = &mv78xx0_mbus_data, },
  641. { },
  642. };
  643. /*
  644. * Public API of the driver
  645. */
  646. int mvebu_mbus_add_window_remap_by_id(unsigned int target,
  647. unsigned int attribute,
  648. phys_addr_t base, size_t size,
  649. phys_addr_t remap)
  650. {
  651. struct mvebu_mbus_state *s = &mbus_state;
  652. if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
  653. pr_err("cannot add window '%x:%x', conflicts with another window\n",
  654. target, attribute);
  655. return -EINVAL;
  656. }
  657. return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
  658. }
  659. int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
  660. phys_addr_t base, size_t size)
  661. {
  662. return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
  663. size, MVEBU_MBUS_NO_REMAP);
  664. }
  665. int mvebu_mbus_del_window(phys_addr_t base, size_t size)
  666. {
  667. int win;
  668. win = mvebu_mbus_find_window(&mbus_state, base, size);
  669. if (win < 0)
  670. return win;
  671. mvebu_mbus_disable_window(&mbus_state, win);
  672. return 0;
  673. }
  674. void mvebu_mbus_get_pcie_mem_aperture(struct resource *res)
  675. {
  676. if (!res)
  677. return;
  678. *res = mbus_state.pcie_mem_aperture;
  679. }
  680. void mvebu_mbus_get_pcie_io_aperture(struct resource *res)
  681. {
  682. if (!res)
  683. return;
  684. *res = mbus_state.pcie_io_aperture;
  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. static int __init mvebu_mbus_common_init(struct mvebu_mbus_state *mbus,
  709. 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. int win;
  715. mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
  716. if (!mbus->mbuswins_base)
  717. return -ENOMEM;
  718. mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
  719. if (!mbus->sdramwins_base) {
  720. iounmap(mbus_state.mbuswins_base);
  721. return -ENOMEM;
  722. }
  723. if (of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric"))
  724. mbus->hw_io_coherency = 1;
  725. for (win = 0; win < mbus->soc->num_wins; win++)
  726. mvebu_mbus_disable_window(mbus, win);
  727. mbus->soc->setup_cpu_target(mbus);
  728. return 0;
  729. }
  730. int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
  731. size_t mbuswins_size,
  732. phys_addr_t sdramwins_phys_base,
  733. size_t sdramwins_size)
  734. {
  735. const struct of_device_id *of_id;
  736. for (of_id = of_mvebu_mbus_ids; of_id->compatible; of_id++)
  737. if (!strcmp(of_id->compatible, soc))
  738. break;
  739. if (!of_id->compatible) {
  740. pr_err("could not find a matching SoC family\n");
  741. return -ENODEV;
  742. }
  743. mbus_state.soc = of_id->data;
  744. return mvebu_mbus_common_init(&mbus_state,
  745. mbuswins_phys_base,
  746. mbuswins_size,
  747. sdramwins_phys_base,
  748. sdramwins_size);
  749. }
  750. #ifdef CONFIG_OF
  751. /*
  752. * The window IDs in the ranges DT property have the following format:
  753. * - bits 28 to 31: MBus custom field
  754. * - bits 24 to 27: window target ID
  755. * - bits 16 to 23: window attribute ID
  756. * - bits 0 to 15: unused
  757. */
  758. #define CUSTOM(id) (((id) & 0xF0000000) >> 24)
  759. #define TARGET(id) (((id) & 0x0F000000) >> 24)
  760. #define ATTR(id) (((id) & 0x00FF0000) >> 16)
  761. static int __init mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
  762. u32 base, u32 size,
  763. u8 target, u8 attr)
  764. {
  765. const struct mvebu_mbus_mapping *map = mbus->soc->map;
  766. const char *name;
  767. int i;
  768. /* Search for a suitable window in the existing mappings */
  769. for (i = 0; map[i].name; i++)
  770. if (map[i].target == target &&
  771. map[i].attr == (attr & map[i].attrmask))
  772. break;
  773. name = map[i].name;
  774. if (!name) {
  775. pr_err("window 0x%x:0x%x is unknown, skipping\n",
  776. target, attr);
  777. return -EINVAL;
  778. }
  779. if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
  780. pr_err("cannot add window '%s', conflicts with another window\n",
  781. name);
  782. return -EBUSY;
  783. }
  784. if (mvebu_mbus_alloc_window(mbus, base, size, MVEBU_MBUS_NO_REMAP,
  785. target, attr)) {
  786. pr_err("cannot add window '%s', too many windows\n",
  787. name);
  788. return -ENOMEM;
  789. }
  790. return 0;
  791. }
  792. static int __init
  793. mbus_parse_ranges(struct device_node *node,
  794. int *addr_cells, int *c_addr_cells, int *c_size_cells,
  795. int *cell_count, const __be32 **ranges_start,
  796. const __be32 **ranges_end)
  797. {
  798. const __be32 *prop;
  799. int ranges_len, tuple_len;
  800. /* Allow a node with no 'ranges' property */
  801. *ranges_start = of_get_property(node, "ranges", &ranges_len);
  802. if (*ranges_start == NULL) {
  803. *addr_cells = *c_addr_cells = *c_size_cells = *cell_count = 0;
  804. *ranges_start = *ranges_end = NULL;
  805. return 0;
  806. }
  807. *ranges_end = *ranges_start + ranges_len / sizeof(__be32);
  808. *addr_cells = of_n_addr_cells(node);
  809. prop = of_get_property(node, "#address-cells", NULL);
  810. *c_addr_cells = be32_to_cpup(prop);
  811. prop = of_get_property(node, "#size-cells", NULL);
  812. *c_size_cells = be32_to_cpup(prop);
  813. *cell_count = *addr_cells + *c_addr_cells + *c_size_cells;
  814. tuple_len = (*cell_count) * sizeof(__be32);
  815. if (ranges_len % tuple_len) {
  816. pr_warn("malformed ranges entry '%s'\n", node->name);
  817. return -EINVAL;
  818. }
  819. return 0;
  820. }
  821. static int __init mbus_dt_setup(struct mvebu_mbus_state *mbus,
  822. struct device_node *np)
  823. {
  824. int addr_cells, c_addr_cells, c_size_cells;
  825. int i, ret, cell_count;
  826. const __be32 *r, *ranges_start, *ranges_end;
  827. ret = mbus_parse_ranges(np, &addr_cells, &c_addr_cells,
  828. &c_size_cells, &cell_count,
  829. &ranges_start, &ranges_end);
  830. if (ret < 0)
  831. return ret;
  832. for (i = 0, r = ranges_start; r < ranges_end; r += cell_count, i++) {
  833. u32 windowid, base, size;
  834. u8 target, attr;
  835. /*
  836. * An entry with a non-zero custom field do not
  837. * correspond to a static window, so skip it.
  838. */
  839. windowid = of_read_number(r, 1);
  840. if (CUSTOM(windowid))
  841. continue;
  842. target = TARGET(windowid);
  843. attr = ATTR(windowid);
  844. base = of_read_number(r + c_addr_cells, addr_cells);
  845. size = of_read_number(r + c_addr_cells + addr_cells,
  846. c_size_cells);
  847. ret = mbus_dt_setup_win(mbus, base, size, target, attr);
  848. if (ret < 0)
  849. return ret;
  850. }
  851. return 0;
  852. }
  853. static void __init mvebu_mbus_get_pcie_resources(struct device_node *np,
  854. struct resource *mem,
  855. struct resource *io)
  856. {
  857. u32 reg[2];
  858. int ret;
  859. /*
  860. * These are optional, so we clear them and they'll
  861. * be zero if they are missing from the DT.
  862. */
  863. memset(mem, 0, sizeof(struct resource));
  864. memset(io, 0, sizeof(struct resource));
  865. ret = of_property_read_u32_array(np, "pcie-mem-aperture", reg, ARRAY_SIZE(reg));
  866. if (!ret) {
  867. mem->start = reg[0];
  868. mem->end = mem->start + reg[1];
  869. mem->flags = IORESOURCE_MEM;
  870. }
  871. ret = of_property_read_u32_array(np, "pcie-io-aperture", reg, ARRAY_SIZE(reg));
  872. if (!ret) {
  873. io->start = reg[0];
  874. io->end = io->start + reg[1];
  875. io->flags = IORESOURCE_IO;
  876. }
  877. }
  878. int __init mvebu_mbus_dt_init(void)
  879. {
  880. struct resource mbuswins_res, sdramwins_res;
  881. struct device_node *np, *controller;
  882. const struct of_device_id *of_id;
  883. const __be32 *prop;
  884. int ret;
  885. np = of_find_matching_node(NULL, of_mvebu_mbus_ids);
  886. if (!np) {
  887. pr_err("could not find a matching SoC family\n");
  888. return -ENODEV;
  889. }
  890. of_id = of_match_node(of_mvebu_mbus_ids, np);
  891. mbus_state.soc = of_id->data;
  892. prop = of_get_property(np, "controller", NULL);
  893. if (!prop) {
  894. pr_err("required 'controller' property missing\n");
  895. return -EINVAL;
  896. }
  897. controller = of_find_node_by_phandle(be32_to_cpup(prop));
  898. if (!controller) {
  899. pr_err("could not find an 'mbus-controller' node\n");
  900. return -ENODEV;
  901. }
  902. if (of_address_to_resource(controller, 0, &mbuswins_res)) {
  903. pr_err("cannot get MBUS register address\n");
  904. return -EINVAL;
  905. }
  906. if (of_address_to_resource(controller, 1, &sdramwins_res)) {
  907. pr_err("cannot get SDRAM register address\n");
  908. return -EINVAL;
  909. }
  910. /* Get optional pcie-{mem,io}-aperture properties */
  911. mvebu_mbus_get_pcie_resources(np, &mbus_state.pcie_mem_aperture,
  912. &mbus_state.pcie_io_aperture);
  913. ret = mvebu_mbus_common_init(&mbus_state,
  914. mbuswins_res.start,
  915. resource_size(&mbuswins_res),
  916. sdramwins_res.start,
  917. resource_size(&sdramwins_res));
  918. if (ret)
  919. return ret;
  920. /* Setup statically declared windows in the DT */
  921. return mbus_dt_setup(&mbus_state, np);
  922. }
  923. #endif