mvebu-mbus.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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_xp_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 kirkwood_mbus_data = {
  460. .num_wins = 8,
  461. .num_remappable_wins = 4,
  462. .win_cfg_offset = orion_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 dove_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_dove_setup_cpu_target,
  471. .show_cpu_target = mvebu_sdram_debug_show_dove,
  472. };
  473. /*
  474. * Some variants of Orion5x have 4 remappable windows, some other have
  475. * only two of them.
  476. */
  477. static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
  478. .num_wins = 8,
  479. .num_remappable_wins = 4,
  480. .win_cfg_offset = orion_mbus_win_offset,
  481. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  482. .show_cpu_target = mvebu_sdram_debug_show_orion,
  483. };
  484. static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
  485. .num_wins = 8,
  486. .num_remappable_wins = 2,
  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 mv78xx0_mbus_data = {
  492. .num_wins = 14,
  493. .num_remappable_wins = 8,
  494. .win_cfg_offset = mv78xx0_mbus_win_offset,
  495. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  496. .show_cpu_target = mvebu_sdram_debug_show_orion,
  497. };
  498. /*
  499. * The driver doesn't yet have a DT binding because the details of
  500. * this DT binding still need to be sorted out. However, as a
  501. * preparation, we already use of_device_id to match a SoC description
  502. * string against the SoC specific details of this driver.
  503. */
  504. static const struct of_device_id of_mvebu_mbus_ids[] = {
  505. { .compatible = "marvell,armada370-mbus",
  506. .data = &armada_370_xp_mbus_data, },
  507. { .compatible = "marvell,armadaxp-mbus",
  508. .data = &armada_370_xp_mbus_data, },
  509. { .compatible = "marvell,kirkwood-mbus",
  510. .data = &kirkwood_mbus_data, },
  511. { .compatible = "marvell,dove-mbus",
  512. .data = &dove_mbus_data, },
  513. { .compatible = "marvell,orion5x-88f5281-mbus",
  514. .data = &orion5x_4win_mbus_data, },
  515. { .compatible = "marvell,orion5x-88f5182-mbus",
  516. .data = &orion5x_2win_mbus_data, },
  517. { .compatible = "marvell,orion5x-88f5181-mbus",
  518. .data = &orion5x_2win_mbus_data, },
  519. { .compatible = "marvell,orion5x-88f6183-mbus",
  520. .data = &orion5x_4win_mbus_data, },
  521. { .compatible = "marvell,mv78xx0-mbus",
  522. .data = &mv78xx0_mbus_data, },
  523. { },
  524. };
  525. /*
  526. * Public API of the driver
  527. */
  528. int mvebu_mbus_add_window_remap_by_id(unsigned int target,
  529. unsigned int attribute,
  530. phys_addr_t base, size_t size,
  531. phys_addr_t remap)
  532. {
  533. struct mvebu_mbus_state *s = &mbus_state;
  534. if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
  535. pr_err("cannot add window '%x:%x', conflicts with another window\n",
  536. target, attribute);
  537. return -EINVAL;
  538. }
  539. return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
  540. }
  541. int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
  542. phys_addr_t base, size_t size)
  543. {
  544. return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
  545. size, MVEBU_MBUS_NO_REMAP);
  546. }
  547. int mvebu_mbus_del_window(phys_addr_t base, size_t size)
  548. {
  549. int win;
  550. win = mvebu_mbus_find_window(&mbus_state, base, size);
  551. if (win < 0)
  552. return win;
  553. mvebu_mbus_disable_window(&mbus_state, win);
  554. return 0;
  555. }
  556. void mvebu_mbus_get_pcie_mem_aperture(struct resource *res)
  557. {
  558. if (!res)
  559. return;
  560. *res = mbus_state.pcie_mem_aperture;
  561. }
  562. void mvebu_mbus_get_pcie_io_aperture(struct resource *res)
  563. {
  564. if (!res)
  565. return;
  566. *res = mbus_state.pcie_io_aperture;
  567. }
  568. static __init int mvebu_mbus_debugfs_init(void)
  569. {
  570. struct mvebu_mbus_state *s = &mbus_state;
  571. /*
  572. * If no base has been initialized, doesn't make sense to
  573. * register the debugfs entries. We may be on a multiplatform
  574. * kernel that isn't running a Marvell EBU SoC.
  575. */
  576. if (!s->mbuswins_base)
  577. return 0;
  578. s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
  579. if (s->debugfs_root) {
  580. s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
  581. s->debugfs_root, NULL,
  582. &mvebu_sdram_debug_fops);
  583. s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
  584. s->debugfs_root, NULL,
  585. &mvebu_devs_debug_fops);
  586. }
  587. return 0;
  588. }
  589. fs_initcall(mvebu_mbus_debugfs_init);
  590. static int __init mvebu_mbus_common_init(struct mvebu_mbus_state *mbus,
  591. phys_addr_t mbuswins_phys_base,
  592. size_t mbuswins_size,
  593. phys_addr_t sdramwins_phys_base,
  594. size_t sdramwins_size)
  595. {
  596. int win;
  597. mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
  598. if (!mbus->mbuswins_base)
  599. return -ENOMEM;
  600. mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
  601. if (!mbus->sdramwins_base) {
  602. iounmap(mbus_state.mbuswins_base);
  603. return -ENOMEM;
  604. }
  605. if (of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric"))
  606. mbus->hw_io_coherency = 1;
  607. for (win = 0; win < mbus->soc->num_wins; win++)
  608. mvebu_mbus_disable_window(mbus, win);
  609. mbus->soc->setup_cpu_target(mbus);
  610. return 0;
  611. }
  612. int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
  613. size_t mbuswins_size,
  614. phys_addr_t sdramwins_phys_base,
  615. size_t sdramwins_size)
  616. {
  617. const struct of_device_id *of_id;
  618. for (of_id = of_mvebu_mbus_ids; of_id->compatible; of_id++)
  619. if (!strcmp(of_id->compatible, soc))
  620. break;
  621. if (!of_id->compatible) {
  622. pr_err("could not find a matching SoC family\n");
  623. return -ENODEV;
  624. }
  625. mbus_state.soc = of_id->data;
  626. return mvebu_mbus_common_init(&mbus_state,
  627. mbuswins_phys_base,
  628. mbuswins_size,
  629. sdramwins_phys_base,
  630. sdramwins_size);
  631. }
  632. #ifdef CONFIG_OF
  633. /*
  634. * The window IDs in the ranges DT property have the following format:
  635. * - bits 28 to 31: MBus custom field
  636. * - bits 24 to 27: window target ID
  637. * - bits 16 to 23: window attribute ID
  638. * - bits 0 to 15: unused
  639. */
  640. #define CUSTOM(id) (((id) & 0xF0000000) >> 24)
  641. #define TARGET(id) (((id) & 0x0F000000) >> 24)
  642. #define ATTR(id) (((id) & 0x00FF0000) >> 16)
  643. static int __init mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
  644. u32 base, u32 size,
  645. u8 target, u8 attr)
  646. {
  647. if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
  648. pr_err("cannot add window '%04x:%04x', conflicts with another window\n",
  649. target, attr);
  650. return -EBUSY;
  651. }
  652. if (mvebu_mbus_alloc_window(mbus, base, size, MVEBU_MBUS_NO_REMAP,
  653. target, attr)) {
  654. pr_err("cannot add window '%04x:%04x', too many windows\n",
  655. target, attr);
  656. return -ENOMEM;
  657. }
  658. return 0;
  659. }
  660. static int __init
  661. mbus_parse_ranges(struct device_node *node,
  662. int *addr_cells, int *c_addr_cells, int *c_size_cells,
  663. int *cell_count, const __be32 **ranges_start,
  664. const __be32 **ranges_end)
  665. {
  666. const __be32 *prop;
  667. int ranges_len, tuple_len;
  668. /* Allow a node with no 'ranges' property */
  669. *ranges_start = of_get_property(node, "ranges", &ranges_len);
  670. if (*ranges_start == NULL) {
  671. *addr_cells = *c_addr_cells = *c_size_cells = *cell_count = 0;
  672. *ranges_start = *ranges_end = NULL;
  673. return 0;
  674. }
  675. *ranges_end = *ranges_start + ranges_len / sizeof(__be32);
  676. *addr_cells = of_n_addr_cells(node);
  677. prop = of_get_property(node, "#address-cells", NULL);
  678. *c_addr_cells = be32_to_cpup(prop);
  679. prop = of_get_property(node, "#size-cells", NULL);
  680. *c_size_cells = be32_to_cpup(prop);
  681. *cell_count = *addr_cells + *c_addr_cells + *c_size_cells;
  682. tuple_len = (*cell_count) * sizeof(__be32);
  683. if (ranges_len % tuple_len) {
  684. pr_warn("malformed ranges entry '%s'\n", node->name);
  685. return -EINVAL;
  686. }
  687. return 0;
  688. }
  689. static int __init mbus_dt_setup(struct mvebu_mbus_state *mbus,
  690. struct device_node *np)
  691. {
  692. int addr_cells, c_addr_cells, c_size_cells;
  693. int i, ret, cell_count;
  694. const __be32 *r, *ranges_start, *ranges_end;
  695. ret = mbus_parse_ranges(np, &addr_cells, &c_addr_cells,
  696. &c_size_cells, &cell_count,
  697. &ranges_start, &ranges_end);
  698. if (ret < 0)
  699. return ret;
  700. for (i = 0, r = ranges_start; r < ranges_end; r += cell_count, i++) {
  701. u32 windowid, base, size;
  702. u8 target, attr;
  703. /*
  704. * An entry with a non-zero custom field do not
  705. * correspond to a static window, so skip it.
  706. */
  707. windowid = of_read_number(r, 1);
  708. if (CUSTOM(windowid))
  709. continue;
  710. target = TARGET(windowid);
  711. attr = ATTR(windowid);
  712. base = of_read_number(r + c_addr_cells, addr_cells);
  713. size = of_read_number(r + c_addr_cells + addr_cells,
  714. c_size_cells);
  715. ret = mbus_dt_setup_win(mbus, base, size, target, attr);
  716. if (ret < 0)
  717. return ret;
  718. }
  719. return 0;
  720. }
  721. static void __init mvebu_mbus_get_pcie_resources(struct device_node *np,
  722. struct resource *mem,
  723. struct resource *io)
  724. {
  725. u32 reg[2];
  726. int ret;
  727. /*
  728. * These are optional, so we clear them and they'll
  729. * be zero if they are missing from the DT.
  730. */
  731. memset(mem, 0, sizeof(struct resource));
  732. memset(io, 0, sizeof(struct resource));
  733. ret = of_property_read_u32_array(np, "pcie-mem-aperture", reg, ARRAY_SIZE(reg));
  734. if (!ret) {
  735. mem->start = reg[0];
  736. mem->end = mem->start + reg[1];
  737. mem->flags = IORESOURCE_MEM;
  738. }
  739. ret = of_property_read_u32_array(np, "pcie-io-aperture", reg, ARRAY_SIZE(reg));
  740. if (!ret) {
  741. io->start = reg[0];
  742. io->end = io->start + reg[1];
  743. io->flags = IORESOURCE_IO;
  744. }
  745. }
  746. int __init mvebu_mbus_dt_init(void)
  747. {
  748. struct resource mbuswins_res, sdramwins_res;
  749. struct device_node *np, *controller;
  750. const struct of_device_id *of_id;
  751. const __be32 *prop;
  752. int ret;
  753. np = of_find_matching_node(NULL, of_mvebu_mbus_ids);
  754. if (!np) {
  755. pr_err("could not find a matching SoC family\n");
  756. return -ENODEV;
  757. }
  758. of_id = of_match_node(of_mvebu_mbus_ids, np);
  759. mbus_state.soc = of_id->data;
  760. prop = of_get_property(np, "controller", NULL);
  761. if (!prop) {
  762. pr_err("required 'controller' property missing\n");
  763. return -EINVAL;
  764. }
  765. controller = of_find_node_by_phandle(be32_to_cpup(prop));
  766. if (!controller) {
  767. pr_err("could not find an 'mbus-controller' node\n");
  768. return -ENODEV;
  769. }
  770. if (of_address_to_resource(controller, 0, &mbuswins_res)) {
  771. pr_err("cannot get MBUS register address\n");
  772. return -EINVAL;
  773. }
  774. if (of_address_to_resource(controller, 1, &sdramwins_res)) {
  775. pr_err("cannot get SDRAM register address\n");
  776. return -EINVAL;
  777. }
  778. /* Get optional pcie-{mem,io}-aperture properties */
  779. mvebu_mbus_get_pcie_resources(np, &mbus_state.pcie_mem_aperture,
  780. &mbus_state.pcie_io_aperture);
  781. ret = mvebu_mbus_common_init(&mbus_state,
  782. mbuswins_res.start,
  783. resource_size(&mbuswins_res),
  784. sdramwins_res.start,
  785. resource_size(&sdramwins_res));
  786. if (ret)
  787. return ret;
  788. /* Setup statically declared windows in the DT */
  789. return mbus_dt_setup(&mbus_state, np);
  790. }
  791. #endif