mvebu-mbus.c 25 KB

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