mvebu-mbus.c 25 KB

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