mvebu-mbus.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Address map functions for Marvell EBU SoCs (Kirkwood, Armada
  4. * 370/XP, Dove, Orion5x and MV78xx0)
  5. *
  6. * The Marvell EBU SoCs have a configurable physical address space:
  7. * the physical address at which certain devices (PCIe, NOR, NAND,
  8. * etc.) sit can be configured. The configuration takes place through
  9. * two sets of registers:
  10. *
  11. * - One to configure the access of the CPU to the devices. Depending
  12. * on the families, there are between 8 and 20 configurable windows,
  13. * each can be use to create a physical memory window that maps to a
  14. * specific device. Devices are identified by a tuple (target,
  15. * attribute).
  16. *
  17. * - One to configure the access to the CPU to the SDRAM. There are
  18. * either 2 (for Dove) or 4 (for other families) windows to map the
  19. * SDRAM into the physical address space.
  20. *
  21. * This driver:
  22. *
  23. * - Reads out the SDRAM address decoding windows at initialization
  24. * time, and fills the mvebu_mbus_dram_info structure with these
  25. * information. The exported function mv_mbus_dram_info() allow
  26. * device drivers to get those information related to the SDRAM
  27. * address decoding windows. This is because devices also have their
  28. * own windows (configured through registers that are part of each
  29. * device register space), and therefore the drivers for Marvell
  30. * devices have to configure those device -> SDRAM windows to ensure
  31. * that DMA works properly.
  32. *
  33. * - Provides an API for platform code or device drivers to
  34. * dynamically add or remove address decoding windows for the CPU ->
  35. * device accesses. This API is mvebu_mbus_add_window_by_id(),
  36. * mvebu_mbus_add_window_remap_by_id() and
  37. * mvebu_mbus_del_window().
  38. *
  39. * - Provides a debugfs interface in /sys/kernel/debug/mvebu-mbus/ to
  40. * see the list of CPU -> SDRAM windows and their configuration
  41. * (file 'sdram') and the list of CPU -> devices windows and their
  42. * configuration (file 'devices').
  43. */
  44. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45. #include <linux/kernel.h>
  46. #include <linux/module.h>
  47. #include <linux/init.h>
  48. #include <linux/mbus.h>
  49. #include <linux/io.h>
  50. #include <linux/ioport.h>
  51. #include <linux/of.h>
  52. #include <linux/of_address.h>
  53. #include <linux/debugfs.h>
  54. #include <linux/log2.h>
  55. #include <linux/memblock.h>
  56. #include <linux/syscore_ops.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. /* Only on HW I/O coherency capable platforms */
  67. #define WIN_CTRL_SYNCBARRIER BIT(1)
  68. #define WIN_CTRL_TGT_MASK 0xf0
  69. #define WIN_CTRL_TGT_SHIFT 4
  70. #define WIN_CTRL_ATTR_MASK 0xff00
  71. #define WIN_CTRL_ATTR_SHIFT 8
  72. #define WIN_CTRL_SIZE_MASK 0xffff0000
  73. #define WIN_CTRL_SIZE_SHIFT 16
  74. #define WIN_BASE_OFF 0x0004
  75. #define WIN_BASE_LOW 0xffff0000
  76. #define WIN_BASE_HIGH 0xf
  77. #define WIN_REMAP_LO_OFF 0x0008
  78. #define WIN_REMAP_LOW 0xffff0000
  79. #define WIN_REMAP_HI_OFF 0x000c
  80. #define UNIT_SYNC_BARRIER_OFF 0x84
  81. #define UNIT_SYNC_BARRIER_ALL 0xFFFF
  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. /* Relative to mbusbridge_base */
  93. #define MBUS_BRIDGE_CTRL_OFF 0x0
  94. #define MBUS_BRIDGE_BASE_OFF 0x4
  95. /* Maximum number of windows, for all known platforms */
  96. #define MBUS_WINS_MAX 20
  97. struct mvebu_mbus_state;
  98. struct mvebu_mbus_soc_data {
  99. unsigned int num_wins;
  100. bool has_mbus_bridge;
  101. unsigned int (*win_cfg_offset)(const int win);
  102. unsigned int (*win_remap_offset)(const int win);
  103. void (*setup_cpu_target)(struct mvebu_mbus_state *s);
  104. int (*save_cpu_target)(struct mvebu_mbus_state *s,
  105. u32 __iomem *store_addr);
  106. int (*show_cpu_target)(struct mvebu_mbus_state *s,
  107. struct seq_file *seq, void *v);
  108. };
  109. /*
  110. * Used to store the state of one MBus window across suspend/resume.
  111. */
  112. struct mvebu_mbus_win_data {
  113. u32 ctrl;
  114. u32 base;
  115. u32 remap_lo;
  116. u32 remap_hi;
  117. };
  118. struct mvebu_mbus_state {
  119. void __iomem *mbuswins_base;
  120. void __iomem *sdramwins_base;
  121. void __iomem *mbusbridge_base;
  122. phys_addr_t sdramwins_phys_base;
  123. struct dentry *debugfs_root;
  124. struct dentry *debugfs_sdram;
  125. struct dentry *debugfs_devs;
  126. struct resource pcie_mem_aperture;
  127. struct resource pcie_io_aperture;
  128. const struct mvebu_mbus_soc_data *soc;
  129. int hw_io_coherency;
  130. /* Used during suspend/resume */
  131. u32 mbus_bridge_ctrl;
  132. u32 mbus_bridge_base;
  133. struct mvebu_mbus_win_data wins[MBUS_WINS_MAX];
  134. };
  135. static struct mvebu_mbus_state mbus_state;
  136. /*
  137. * We provide two variants of the mv_mbus_dram_info() function:
  138. *
  139. * - The normal one, where the described DRAM ranges may overlap with
  140. * the I/O windows, but for which the DRAM ranges are guaranteed to
  141. * have a power of two size. Such ranges are suitable for the DMA
  142. * masters that only DMA between the RAM and the device, which is
  143. * actually all devices except the crypto engines.
  144. *
  145. * - The 'nooverlap' one, where the described DRAM ranges are
  146. * guaranteed to not overlap with the I/O windows, but for which the
  147. * DRAM ranges will not have power of two sizes. They will only be
  148. * aligned on a 64 KB boundary, and have a size multiple of 64
  149. * KB. Such ranges are suitable for the DMA masters that DMA between
  150. * the crypto SRAM (which is mapped through an I/O window) and a
  151. * device. This is the case for the crypto engines.
  152. */
  153. static struct mbus_dram_target_info mvebu_mbus_dram_info;
  154. static struct mbus_dram_target_info mvebu_mbus_dram_info_nooverlap;
  155. const struct mbus_dram_target_info *mv_mbus_dram_info(void)
  156. {
  157. return &mvebu_mbus_dram_info;
  158. }
  159. EXPORT_SYMBOL_GPL(mv_mbus_dram_info);
  160. const struct mbus_dram_target_info *mv_mbus_dram_info_nooverlap(void)
  161. {
  162. return &mvebu_mbus_dram_info_nooverlap;
  163. }
  164. EXPORT_SYMBOL_GPL(mv_mbus_dram_info_nooverlap);
  165. /* Checks whether the given window has remap capability */
  166. static bool mvebu_mbus_window_is_remappable(struct mvebu_mbus_state *mbus,
  167. const int win)
  168. {
  169. return mbus->soc->win_remap_offset(win) != MVEBU_MBUS_NO_REMAP;
  170. }
  171. /*
  172. * Functions to manipulate the address decoding windows
  173. */
  174. static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
  175. int win, int *enabled, u64 *base,
  176. u32 *size, u8 *target, u8 *attr,
  177. u64 *remap)
  178. {
  179. void __iomem *addr = mbus->mbuswins_base +
  180. mbus->soc->win_cfg_offset(win);
  181. u32 basereg = readl(addr + WIN_BASE_OFF);
  182. u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
  183. if (!(ctrlreg & WIN_CTRL_ENABLE)) {
  184. *enabled = 0;
  185. return;
  186. }
  187. *enabled = 1;
  188. *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
  189. *base |= (basereg & WIN_BASE_LOW);
  190. *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
  191. if (target)
  192. *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
  193. if (attr)
  194. *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
  195. if (remap) {
  196. if (mvebu_mbus_window_is_remappable(mbus, win)) {
  197. u32 remap_low, remap_hi;
  198. void __iomem *addr_rmp = mbus->mbuswins_base +
  199. mbus->soc->win_remap_offset(win);
  200. remap_low = readl(addr_rmp + WIN_REMAP_LO_OFF);
  201. remap_hi = readl(addr_rmp + WIN_REMAP_HI_OFF);
  202. *remap = ((u64)remap_hi << 32) | remap_low;
  203. } else
  204. *remap = 0;
  205. }
  206. }
  207. static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
  208. int win)
  209. {
  210. void __iomem *addr;
  211. addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
  212. writel(0, addr + WIN_BASE_OFF);
  213. writel(0, addr + WIN_CTRL_OFF);
  214. if (mvebu_mbus_window_is_remappable(mbus, win)) {
  215. addr = mbus->mbuswins_base + mbus->soc->win_remap_offset(win);
  216. writel(0, addr + WIN_REMAP_LO_OFF);
  217. writel(0, addr + WIN_REMAP_HI_OFF);
  218. }
  219. }
  220. /* Checks whether the given window number is available */
  221. static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
  222. const int win)
  223. {
  224. void __iomem *addr = mbus->mbuswins_base +
  225. mbus->soc->win_cfg_offset(win);
  226. u32 ctrl = readl(addr + WIN_CTRL_OFF);
  227. return !(ctrl & WIN_CTRL_ENABLE);
  228. }
  229. /*
  230. * Checks whether the given (base, base+size) area doesn't overlap an
  231. * existing region
  232. */
  233. static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
  234. phys_addr_t base, size_t size,
  235. u8 target, u8 attr)
  236. {
  237. u64 end = (u64)base + size;
  238. int win;
  239. for (win = 0; win < mbus->soc->num_wins; win++) {
  240. u64 wbase, wend;
  241. u32 wsize;
  242. u8 wtarget, wattr;
  243. int enabled;
  244. mvebu_mbus_read_window(mbus, win,
  245. &enabled, &wbase, &wsize,
  246. &wtarget, &wattr, NULL);
  247. if (!enabled)
  248. continue;
  249. wend = wbase + wsize;
  250. /*
  251. * Check if the current window overlaps with the
  252. * proposed physical range
  253. */
  254. if ((u64)base < wend && end > wbase)
  255. return 0;
  256. }
  257. return 1;
  258. }
  259. static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
  260. phys_addr_t base, size_t size)
  261. {
  262. int win;
  263. for (win = 0; win < mbus->soc->num_wins; win++) {
  264. u64 wbase;
  265. u32 wsize;
  266. int enabled;
  267. mvebu_mbus_read_window(mbus, win,
  268. &enabled, &wbase, &wsize,
  269. NULL, NULL, NULL);
  270. if (!enabled)
  271. continue;
  272. if (base == wbase && size == wsize)
  273. return win;
  274. }
  275. return -ENODEV;
  276. }
  277. static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
  278. int win, phys_addr_t base, size_t size,
  279. phys_addr_t remap, u8 target,
  280. u8 attr)
  281. {
  282. void __iomem *addr = mbus->mbuswins_base +
  283. mbus->soc->win_cfg_offset(win);
  284. u32 ctrl, remap_addr;
  285. if (!is_power_of_2(size)) {
  286. WARN(true, "Invalid MBus window size: 0x%zx\n", size);
  287. return -EINVAL;
  288. }
  289. if ((base & (phys_addr_t)(size - 1)) != 0) {
  290. WARN(true, "Invalid MBus base/size: %pa len 0x%zx\n", &base,
  291. size);
  292. return -EINVAL;
  293. }
  294. ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
  295. (attr << WIN_CTRL_ATTR_SHIFT) |
  296. (target << WIN_CTRL_TGT_SHIFT) |
  297. WIN_CTRL_ENABLE;
  298. if (mbus->hw_io_coherency)
  299. ctrl |= WIN_CTRL_SYNCBARRIER;
  300. writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
  301. writel(ctrl, addr + WIN_CTRL_OFF);
  302. if (mvebu_mbus_window_is_remappable(mbus, win)) {
  303. void __iomem *addr_rmp = mbus->mbuswins_base +
  304. mbus->soc->win_remap_offset(win);
  305. if (remap == MVEBU_MBUS_NO_REMAP)
  306. remap_addr = base;
  307. else
  308. remap_addr = remap;
  309. writel(remap_addr & WIN_REMAP_LOW, addr_rmp + WIN_REMAP_LO_OFF);
  310. writel(0, addr_rmp + WIN_REMAP_HI_OFF);
  311. }
  312. return 0;
  313. }
  314. static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
  315. phys_addr_t base, size_t size,
  316. phys_addr_t remap, u8 target,
  317. u8 attr)
  318. {
  319. int win;
  320. if (remap == MVEBU_MBUS_NO_REMAP) {
  321. for (win = 0; win < mbus->soc->num_wins; win++) {
  322. if (mvebu_mbus_window_is_remappable(mbus, win))
  323. continue;
  324. if (mvebu_mbus_window_is_free(mbus, win))
  325. return mvebu_mbus_setup_window(mbus, win, base,
  326. size, remap,
  327. target, attr);
  328. }
  329. }
  330. for (win = 0; win < mbus->soc->num_wins; win++) {
  331. /* Skip window if need remap but is not supported */
  332. if ((remap != MVEBU_MBUS_NO_REMAP) &&
  333. !mvebu_mbus_window_is_remappable(mbus, win))
  334. continue;
  335. if (mvebu_mbus_window_is_free(mbus, win))
  336. return mvebu_mbus_setup_window(mbus, win, base, size,
  337. remap, target, attr);
  338. }
  339. return -ENOMEM;
  340. }
  341. /*
  342. * Debugfs debugging
  343. */
  344. /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
  345. static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
  346. struct seq_file *seq, void *v)
  347. {
  348. int i;
  349. for (i = 0; i < 4; i++) {
  350. u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  351. u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  352. u64 base;
  353. u32 size;
  354. if (!(sizereg & DDR_SIZE_ENABLED)) {
  355. seq_printf(seq, "[%d] disabled\n", i);
  356. continue;
  357. }
  358. base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
  359. base |= basereg & DDR_BASE_CS_LOW_MASK;
  360. size = (sizereg | ~DDR_SIZE_MASK);
  361. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  362. i, (unsigned long long)base,
  363. (unsigned long long)base + size + 1,
  364. (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
  365. }
  366. return 0;
  367. }
  368. /* Special function for Dove */
  369. static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
  370. struct seq_file *seq, void *v)
  371. {
  372. int i;
  373. for (i = 0; i < 2; i++) {
  374. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  375. u64 base;
  376. u32 size;
  377. if (!(map & 1)) {
  378. seq_printf(seq, "[%d] disabled\n", i);
  379. continue;
  380. }
  381. base = map & 0xff800000;
  382. size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  383. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  384. i, (unsigned long long)base,
  385. (unsigned long long)base + size, i);
  386. }
  387. return 0;
  388. }
  389. static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
  390. {
  391. struct mvebu_mbus_state *mbus = &mbus_state;
  392. return mbus->soc->show_cpu_target(mbus, seq, v);
  393. }
  394. DEFINE_SHOW_ATTRIBUTE(mvebu_sdram_debug);
  395. static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
  396. {
  397. struct mvebu_mbus_state *mbus = &mbus_state;
  398. int win;
  399. for (win = 0; win < mbus->soc->num_wins; win++) {
  400. u64 wbase, wremap;
  401. u32 wsize;
  402. u8 wtarget, wattr;
  403. int enabled;
  404. mvebu_mbus_read_window(mbus, win,
  405. &enabled, &wbase, &wsize,
  406. &wtarget, &wattr, &wremap);
  407. if (!enabled) {
  408. seq_printf(seq, "[%02d] disabled\n", win);
  409. continue;
  410. }
  411. seq_printf(seq, "[%02d] %016llx - %016llx : %04x:%04x",
  412. win, (unsigned long long)wbase,
  413. (unsigned long long)(wbase + wsize), wtarget, wattr);
  414. if (!is_power_of_2(wsize) ||
  415. ((wbase & (u64)(wsize - 1)) != 0))
  416. seq_puts(seq, " (Invalid base/size!!)");
  417. if (mvebu_mbus_window_is_remappable(mbus, win)) {
  418. seq_printf(seq, " (remap %016llx)\n",
  419. (unsigned long long)wremap);
  420. } else
  421. seq_printf(seq, "\n");
  422. }
  423. return 0;
  424. }
  425. DEFINE_SHOW_ATTRIBUTE(mvebu_devs_debug);
  426. /*
  427. * SoC-specific functions and definitions
  428. */
  429. static unsigned int generic_mbus_win_cfg_offset(int win)
  430. {
  431. return win << 4;
  432. }
  433. static unsigned int armada_370_xp_mbus_win_cfg_offset(int win)
  434. {
  435. /* The register layout is a bit annoying and the below code
  436. * tries to cope with it.
  437. * - At offset 0x0, there are the registers for the first 8
  438. * windows, with 4 registers of 32 bits per window (ctrl,
  439. * base, remap low, remap high)
  440. * - Then at offset 0x80, there is a hole of 0x10 bytes for
  441. * the internal registers base address and internal units
  442. * sync barrier register.
  443. * - Then at offset 0x90, there the registers for 12
  444. * windows, with only 2 registers of 32 bits per window
  445. * (ctrl, base).
  446. */
  447. if (win < 8)
  448. return win << 4;
  449. else
  450. return 0x90 + ((win - 8) << 3);
  451. }
  452. static unsigned int mv78xx0_mbus_win_cfg_offset(int win)
  453. {
  454. if (win < 8)
  455. return win << 4;
  456. else
  457. return 0x900 + ((win - 8) << 4);
  458. }
  459. static unsigned int generic_mbus_win_remap_2_offset(int win)
  460. {
  461. if (win < 2)
  462. return generic_mbus_win_cfg_offset(win);
  463. else
  464. return MVEBU_MBUS_NO_REMAP;
  465. }
  466. static unsigned int generic_mbus_win_remap_4_offset(int win)
  467. {
  468. if (win < 4)
  469. return generic_mbus_win_cfg_offset(win);
  470. else
  471. return MVEBU_MBUS_NO_REMAP;
  472. }
  473. static unsigned int generic_mbus_win_remap_8_offset(int win)
  474. {
  475. if (win < 8)
  476. return generic_mbus_win_cfg_offset(win);
  477. else
  478. return MVEBU_MBUS_NO_REMAP;
  479. }
  480. static unsigned int armada_xp_mbus_win_remap_offset(int win)
  481. {
  482. if (win < 8)
  483. return generic_mbus_win_cfg_offset(win);
  484. else if (win == 13)
  485. return 0xF0 - WIN_REMAP_LO_OFF;
  486. else
  487. return MVEBU_MBUS_NO_REMAP;
  488. }
  489. /*
  490. * Use the memblock information to find the MBus bridge hole in the
  491. * physical address space.
  492. */
  493. static void __init
  494. mvebu_mbus_find_bridge_hole(uint64_t *start, uint64_t *end)
  495. {
  496. phys_addr_t reg_start, reg_end;
  497. uint64_t i, s = 0;
  498. for_each_mem_range(i, &reg_start, &reg_end) {
  499. /*
  500. * This part of the memory is above 4 GB, so we don't
  501. * care for the MBus bridge hole.
  502. */
  503. if ((u64)reg_start >= 0x100000000ULL)
  504. continue;
  505. /*
  506. * The MBus bridge hole is at the end of the RAM under
  507. * the 4 GB limit.
  508. */
  509. if (reg_end > s)
  510. s = reg_end;
  511. }
  512. *start = s;
  513. *end = 0x100000000ULL;
  514. }
  515. /*
  516. * This function fills in the mvebu_mbus_dram_info_nooverlap data
  517. * structure, by looking at the mvebu_mbus_dram_info data, and
  518. * removing the parts of it that overlap with I/O windows.
  519. */
  520. static void __init
  521. mvebu_mbus_setup_cpu_target_nooverlap(struct mvebu_mbus_state *mbus)
  522. {
  523. uint64_t mbus_bridge_base, mbus_bridge_end;
  524. int cs_nooverlap = 0;
  525. int i;
  526. mvebu_mbus_find_bridge_hole(&mbus_bridge_base, &mbus_bridge_end);
  527. for (i = 0; i < mvebu_mbus_dram_info.num_cs; i++) {
  528. struct mbus_dram_window *w;
  529. u64 base, size, end;
  530. w = &mvebu_mbus_dram_info.cs[i];
  531. base = w->base;
  532. size = w->size;
  533. end = base + size;
  534. /*
  535. * The CS is fully enclosed inside the MBus bridge
  536. * area, so ignore it.
  537. */
  538. if (base >= mbus_bridge_base && end <= mbus_bridge_end)
  539. continue;
  540. /*
  541. * Beginning of CS overlaps with end of MBus, raise CS
  542. * base address, and shrink its size.
  543. */
  544. if (base >= mbus_bridge_base && end > mbus_bridge_end) {
  545. size -= mbus_bridge_end - base;
  546. base = mbus_bridge_end;
  547. }
  548. /*
  549. * End of CS overlaps with beginning of MBus, shrink
  550. * CS size.
  551. */
  552. if (base < mbus_bridge_base && end > mbus_bridge_base)
  553. size -= end - mbus_bridge_base;
  554. w = &mvebu_mbus_dram_info_nooverlap.cs[cs_nooverlap++];
  555. w->cs_index = i;
  556. w->mbus_attr = 0xf & ~(1 << i);
  557. if (mbus->hw_io_coherency)
  558. w->mbus_attr |= ATTR_HW_COHERENCY;
  559. w->base = base;
  560. w->size = size;
  561. }
  562. mvebu_mbus_dram_info_nooverlap.mbus_dram_target_id = TARGET_DDR;
  563. mvebu_mbus_dram_info_nooverlap.num_cs = cs_nooverlap;
  564. }
  565. static void __init
  566. mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
  567. {
  568. int i;
  569. int cs;
  570. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  571. for (i = 0, cs = 0; i < 4; i++) {
  572. u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  573. u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  574. /*
  575. * We only take care of entries for which the chip
  576. * select is enabled, and that don't have high base
  577. * address bits set (devices can only access the first
  578. * 32 bits of the memory).
  579. */
  580. if ((size & DDR_SIZE_ENABLED) &&
  581. !(base & DDR_BASE_CS_HIGH_MASK)) {
  582. struct mbus_dram_window *w;
  583. w = &mvebu_mbus_dram_info.cs[cs++];
  584. w->cs_index = i;
  585. w->mbus_attr = 0xf & ~(1 << i);
  586. if (mbus->hw_io_coherency)
  587. w->mbus_attr |= ATTR_HW_COHERENCY;
  588. w->base = base & DDR_BASE_CS_LOW_MASK;
  589. w->size = (u64)(size | ~DDR_SIZE_MASK) + 1;
  590. }
  591. }
  592. mvebu_mbus_dram_info.num_cs = cs;
  593. }
  594. static int
  595. mvebu_mbus_default_save_cpu_target(struct mvebu_mbus_state *mbus,
  596. u32 __iomem *store_addr)
  597. {
  598. int i;
  599. for (i = 0; i < 4; i++) {
  600. u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  601. u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  602. writel(mbus->sdramwins_phys_base + DDR_BASE_CS_OFF(i),
  603. store_addr++);
  604. writel(base, store_addr++);
  605. writel(mbus->sdramwins_phys_base + DDR_SIZE_CS_OFF(i),
  606. store_addr++);
  607. writel(size, store_addr++);
  608. }
  609. /* We've written 16 words to the store address */
  610. return 16;
  611. }
  612. static void __init
  613. mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
  614. {
  615. int i;
  616. int cs;
  617. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  618. for (i = 0, cs = 0; i < 2; i++) {
  619. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  620. /*
  621. * Chip select enabled?
  622. */
  623. if (map & 1) {
  624. struct mbus_dram_window *w;
  625. w = &mvebu_mbus_dram_info.cs[cs++];
  626. w->cs_index = i;
  627. w->mbus_attr = 0; /* CS address decoding done inside */
  628. /* the DDR controller, no need to */
  629. /* provide attributes */
  630. w->base = map & 0xff800000;
  631. w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  632. }
  633. }
  634. mvebu_mbus_dram_info.num_cs = cs;
  635. }
  636. static int
  637. mvebu_mbus_dove_save_cpu_target(struct mvebu_mbus_state *mbus,
  638. u32 __iomem *store_addr)
  639. {
  640. int i;
  641. for (i = 0; i < 2; i++) {
  642. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  643. writel(mbus->sdramwins_phys_base + DOVE_DDR_BASE_CS_OFF(i),
  644. store_addr++);
  645. writel(map, store_addr++);
  646. }
  647. /* We've written 4 words to the store address */
  648. return 4;
  649. }
  650. int mvebu_mbus_save_cpu_target(u32 __iomem *store_addr)
  651. {
  652. return mbus_state.soc->save_cpu_target(&mbus_state, store_addr);
  653. }
  654. static const struct mvebu_mbus_soc_data armada_370_mbus_data = {
  655. .num_wins = 20,
  656. .has_mbus_bridge = true,
  657. .win_cfg_offset = armada_370_xp_mbus_win_cfg_offset,
  658. .win_remap_offset = generic_mbus_win_remap_8_offset,
  659. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  660. .show_cpu_target = mvebu_sdram_debug_show_orion,
  661. .save_cpu_target = mvebu_mbus_default_save_cpu_target,
  662. };
  663. static const struct mvebu_mbus_soc_data armada_xp_mbus_data = {
  664. .num_wins = 20,
  665. .has_mbus_bridge = true,
  666. .win_cfg_offset = armada_370_xp_mbus_win_cfg_offset,
  667. .win_remap_offset = armada_xp_mbus_win_remap_offset,
  668. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  669. .show_cpu_target = mvebu_sdram_debug_show_orion,
  670. .save_cpu_target = mvebu_mbus_default_save_cpu_target,
  671. };
  672. static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
  673. .num_wins = 8,
  674. .win_cfg_offset = generic_mbus_win_cfg_offset,
  675. .save_cpu_target = mvebu_mbus_default_save_cpu_target,
  676. .win_remap_offset = generic_mbus_win_remap_4_offset,
  677. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  678. .show_cpu_target = mvebu_sdram_debug_show_orion,
  679. };
  680. static const struct mvebu_mbus_soc_data dove_mbus_data = {
  681. .num_wins = 8,
  682. .win_cfg_offset = generic_mbus_win_cfg_offset,
  683. .save_cpu_target = mvebu_mbus_dove_save_cpu_target,
  684. .win_remap_offset = generic_mbus_win_remap_4_offset,
  685. .setup_cpu_target = mvebu_mbus_dove_setup_cpu_target,
  686. .show_cpu_target = mvebu_sdram_debug_show_dove,
  687. };
  688. /*
  689. * Some variants of Orion5x have 4 remappable windows, some other have
  690. * only two of them.
  691. */
  692. static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
  693. .num_wins = 8,
  694. .win_cfg_offset = generic_mbus_win_cfg_offset,
  695. .save_cpu_target = mvebu_mbus_default_save_cpu_target,
  696. .win_remap_offset = generic_mbus_win_remap_4_offset,
  697. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  698. .show_cpu_target = mvebu_sdram_debug_show_orion,
  699. };
  700. static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
  701. .num_wins = 8,
  702. .win_cfg_offset = generic_mbus_win_cfg_offset,
  703. .save_cpu_target = mvebu_mbus_default_save_cpu_target,
  704. .win_remap_offset = generic_mbus_win_remap_2_offset,
  705. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  706. .show_cpu_target = mvebu_sdram_debug_show_orion,
  707. };
  708. static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
  709. .num_wins = 14,
  710. .win_cfg_offset = mv78xx0_mbus_win_cfg_offset,
  711. .save_cpu_target = mvebu_mbus_default_save_cpu_target,
  712. .win_remap_offset = generic_mbus_win_remap_8_offset,
  713. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  714. .show_cpu_target = mvebu_sdram_debug_show_orion,
  715. };
  716. static const struct of_device_id of_mvebu_mbus_ids[] = {
  717. { .compatible = "marvell,armada370-mbus",
  718. .data = &armada_370_mbus_data, },
  719. { .compatible = "marvell,armada375-mbus",
  720. .data = &armada_xp_mbus_data, },
  721. { .compatible = "marvell,armada380-mbus",
  722. .data = &armada_xp_mbus_data, },
  723. { .compatible = "marvell,armadaxp-mbus",
  724. .data = &armada_xp_mbus_data, },
  725. { .compatible = "marvell,kirkwood-mbus",
  726. .data = &kirkwood_mbus_data, },
  727. { .compatible = "marvell,dove-mbus",
  728. .data = &dove_mbus_data, },
  729. { .compatible = "marvell,orion5x-88f5281-mbus",
  730. .data = &orion5x_4win_mbus_data, },
  731. { .compatible = "marvell,orion5x-88f5182-mbus",
  732. .data = &orion5x_2win_mbus_data, },
  733. { .compatible = "marvell,orion5x-88f5181-mbus",
  734. .data = &orion5x_2win_mbus_data, },
  735. { .compatible = "marvell,orion5x-88f6183-mbus",
  736. .data = &orion5x_4win_mbus_data, },
  737. { .compatible = "marvell,mv78xx0-mbus",
  738. .data = &mv78xx0_mbus_data, },
  739. { },
  740. };
  741. /*
  742. * Public API of the driver
  743. */
  744. int mvebu_mbus_add_window_remap_by_id(unsigned int target,
  745. unsigned int attribute,
  746. phys_addr_t base, size_t size,
  747. phys_addr_t remap)
  748. {
  749. struct mvebu_mbus_state *s = &mbus_state;
  750. if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
  751. pr_err("cannot add window '%x:%x', conflicts with another window\n",
  752. target, attribute);
  753. return -EINVAL;
  754. }
  755. return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
  756. }
  757. EXPORT_SYMBOL_GPL(mvebu_mbus_add_window_remap_by_id);
  758. int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
  759. phys_addr_t base, size_t size)
  760. {
  761. return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
  762. size, MVEBU_MBUS_NO_REMAP);
  763. }
  764. EXPORT_SYMBOL_GPL(mvebu_mbus_add_window_by_id);
  765. int mvebu_mbus_del_window(phys_addr_t base, size_t size)
  766. {
  767. int win;
  768. win = mvebu_mbus_find_window(&mbus_state, base, size);
  769. if (win < 0)
  770. return win;
  771. mvebu_mbus_disable_window(&mbus_state, win);
  772. return 0;
  773. }
  774. EXPORT_SYMBOL_GPL(mvebu_mbus_del_window);
  775. void mvebu_mbus_get_pcie_mem_aperture(struct resource *res)
  776. {
  777. if (!res)
  778. return;
  779. *res = mbus_state.pcie_mem_aperture;
  780. }
  781. EXPORT_SYMBOL_GPL(mvebu_mbus_get_pcie_mem_aperture);
  782. void mvebu_mbus_get_pcie_io_aperture(struct resource *res)
  783. {
  784. if (!res)
  785. return;
  786. *res = mbus_state.pcie_io_aperture;
  787. }
  788. EXPORT_SYMBOL_GPL(mvebu_mbus_get_pcie_io_aperture);
  789. int mvebu_mbus_get_dram_win_info(phys_addr_t phyaddr, u8 *target, u8 *attr)
  790. {
  791. const struct mbus_dram_target_info *dram;
  792. int i;
  793. /* Get dram info */
  794. dram = mv_mbus_dram_info();
  795. if (!dram) {
  796. pr_err("missing DRAM information\n");
  797. return -ENODEV;
  798. }
  799. /* Try to find matching DRAM window for phyaddr */
  800. for (i = 0; i < dram->num_cs; i++) {
  801. const struct mbus_dram_window *cs = dram->cs + i;
  802. if (cs->base <= phyaddr &&
  803. phyaddr <= (cs->base + cs->size - 1)) {
  804. *target = dram->mbus_dram_target_id;
  805. *attr = cs->mbus_attr;
  806. return 0;
  807. }
  808. }
  809. pr_err("invalid dram address %pa\n", &phyaddr);
  810. return -EINVAL;
  811. }
  812. EXPORT_SYMBOL_GPL(mvebu_mbus_get_dram_win_info);
  813. int mvebu_mbus_get_io_win_info(phys_addr_t phyaddr, u32 *size, u8 *target,
  814. u8 *attr)
  815. {
  816. int win;
  817. for (win = 0; win < mbus_state.soc->num_wins; win++) {
  818. u64 wbase;
  819. int enabled;
  820. mvebu_mbus_read_window(&mbus_state, win, &enabled, &wbase,
  821. size, target, attr, NULL);
  822. if (!enabled)
  823. continue;
  824. if (wbase <= phyaddr && phyaddr <= wbase + *size)
  825. return win;
  826. }
  827. return -EINVAL;
  828. }
  829. EXPORT_SYMBOL_GPL(mvebu_mbus_get_io_win_info);
  830. static __init int mvebu_mbus_debugfs_init(void)
  831. {
  832. struct mvebu_mbus_state *s = &mbus_state;
  833. /*
  834. * If no base has been initialized, doesn't make sense to
  835. * register the debugfs entries. We may be on a multiplatform
  836. * kernel that isn't running a Marvell EBU SoC.
  837. */
  838. if (!s->mbuswins_base)
  839. return 0;
  840. s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
  841. if (s->debugfs_root) {
  842. s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
  843. s->debugfs_root, NULL,
  844. &mvebu_sdram_debug_fops);
  845. s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
  846. s->debugfs_root, NULL,
  847. &mvebu_devs_debug_fops);
  848. }
  849. return 0;
  850. }
  851. fs_initcall(mvebu_mbus_debugfs_init);
  852. static int mvebu_mbus_suspend(void)
  853. {
  854. struct mvebu_mbus_state *s = &mbus_state;
  855. int win;
  856. if (!s->mbusbridge_base)
  857. return -ENODEV;
  858. for (win = 0; win < s->soc->num_wins; win++) {
  859. void __iomem *addr = s->mbuswins_base +
  860. s->soc->win_cfg_offset(win);
  861. void __iomem *addr_rmp;
  862. s->wins[win].base = readl(addr + WIN_BASE_OFF);
  863. s->wins[win].ctrl = readl(addr + WIN_CTRL_OFF);
  864. if (!mvebu_mbus_window_is_remappable(s, win))
  865. continue;
  866. addr_rmp = s->mbuswins_base +
  867. s->soc->win_remap_offset(win);
  868. s->wins[win].remap_lo = readl(addr_rmp + WIN_REMAP_LO_OFF);
  869. s->wins[win].remap_hi = readl(addr_rmp + WIN_REMAP_HI_OFF);
  870. }
  871. s->mbus_bridge_ctrl = readl(s->mbusbridge_base +
  872. MBUS_BRIDGE_CTRL_OFF);
  873. s->mbus_bridge_base = readl(s->mbusbridge_base +
  874. MBUS_BRIDGE_BASE_OFF);
  875. return 0;
  876. }
  877. static void mvebu_mbus_resume(void)
  878. {
  879. struct mvebu_mbus_state *s = &mbus_state;
  880. int win;
  881. writel(s->mbus_bridge_ctrl,
  882. s->mbusbridge_base + MBUS_BRIDGE_CTRL_OFF);
  883. writel(s->mbus_bridge_base,
  884. s->mbusbridge_base + MBUS_BRIDGE_BASE_OFF);
  885. for (win = 0; win < s->soc->num_wins; win++) {
  886. void __iomem *addr = s->mbuswins_base +
  887. s->soc->win_cfg_offset(win);
  888. void __iomem *addr_rmp;
  889. writel(s->wins[win].base, addr + WIN_BASE_OFF);
  890. writel(s->wins[win].ctrl, addr + WIN_CTRL_OFF);
  891. if (!mvebu_mbus_window_is_remappable(s, win))
  892. continue;
  893. addr_rmp = s->mbuswins_base +
  894. s->soc->win_remap_offset(win);
  895. writel(s->wins[win].remap_lo, addr_rmp + WIN_REMAP_LO_OFF);
  896. writel(s->wins[win].remap_hi, addr_rmp + WIN_REMAP_HI_OFF);
  897. }
  898. }
  899. static struct syscore_ops mvebu_mbus_syscore_ops = {
  900. .suspend = mvebu_mbus_suspend,
  901. .resume = mvebu_mbus_resume,
  902. };
  903. static int __init mvebu_mbus_common_init(struct mvebu_mbus_state *mbus,
  904. phys_addr_t mbuswins_phys_base,
  905. size_t mbuswins_size,
  906. phys_addr_t sdramwins_phys_base,
  907. size_t sdramwins_size,
  908. phys_addr_t mbusbridge_phys_base,
  909. size_t mbusbridge_size,
  910. bool is_coherent)
  911. {
  912. int win;
  913. mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
  914. if (!mbus->mbuswins_base)
  915. return -ENOMEM;
  916. mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
  917. if (!mbus->sdramwins_base) {
  918. iounmap(mbus->mbuswins_base);
  919. return -ENOMEM;
  920. }
  921. mbus->sdramwins_phys_base = sdramwins_phys_base;
  922. if (mbusbridge_phys_base) {
  923. mbus->mbusbridge_base = ioremap(mbusbridge_phys_base,
  924. mbusbridge_size);
  925. if (!mbus->mbusbridge_base) {
  926. iounmap(mbus->sdramwins_base);
  927. iounmap(mbus->mbuswins_base);
  928. return -ENOMEM;
  929. }
  930. } else
  931. mbus->mbusbridge_base = NULL;
  932. for (win = 0; win < mbus->soc->num_wins; win++)
  933. mvebu_mbus_disable_window(mbus, win);
  934. mbus->soc->setup_cpu_target(mbus);
  935. mvebu_mbus_setup_cpu_target_nooverlap(mbus);
  936. if (is_coherent)
  937. writel(UNIT_SYNC_BARRIER_ALL,
  938. mbus->mbuswins_base + UNIT_SYNC_BARRIER_OFF);
  939. register_syscore_ops(&mvebu_mbus_syscore_ops);
  940. return 0;
  941. }
  942. int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
  943. size_t mbuswins_size,
  944. phys_addr_t sdramwins_phys_base,
  945. size_t sdramwins_size)
  946. {
  947. const struct of_device_id *of_id;
  948. for (of_id = of_mvebu_mbus_ids; of_id->compatible[0]; of_id++)
  949. if (!strcmp(of_id->compatible, soc))
  950. break;
  951. if (!of_id->compatible[0]) {
  952. pr_err("could not find a matching SoC family\n");
  953. return -ENODEV;
  954. }
  955. mbus_state.soc = of_id->data;
  956. return mvebu_mbus_common_init(&mbus_state,
  957. mbuswins_phys_base,
  958. mbuswins_size,
  959. sdramwins_phys_base,
  960. sdramwins_size, 0, 0, false);
  961. }
  962. #ifdef CONFIG_OF
  963. /*
  964. * The window IDs in the ranges DT property have the following format:
  965. * - bits 28 to 31: MBus custom field
  966. * - bits 24 to 27: window target ID
  967. * - bits 16 to 23: window attribute ID
  968. * - bits 0 to 15: unused
  969. */
  970. #define CUSTOM(id) (((id) & 0xF0000000) >> 24)
  971. #define TARGET(id) (((id) & 0x0F000000) >> 24)
  972. #define ATTR(id) (((id) & 0x00FF0000) >> 16)
  973. static int __init mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
  974. u32 base, u32 size,
  975. u8 target, u8 attr)
  976. {
  977. if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
  978. pr_err("cannot add window '%04x:%04x', conflicts with another window\n",
  979. target, attr);
  980. return -EBUSY;
  981. }
  982. if (mvebu_mbus_alloc_window(mbus, base, size, MVEBU_MBUS_NO_REMAP,
  983. target, attr)) {
  984. pr_err("cannot add window '%04x:%04x', too many windows\n",
  985. target, attr);
  986. return -ENOMEM;
  987. }
  988. return 0;
  989. }
  990. static int __init
  991. mbus_parse_ranges(struct device_node *node,
  992. int *addr_cells, int *c_addr_cells, int *c_size_cells,
  993. int *cell_count, const __be32 **ranges_start,
  994. const __be32 **ranges_end)
  995. {
  996. const __be32 *prop;
  997. int ranges_len, tuple_len;
  998. /* Allow a node with no 'ranges' property */
  999. *ranges_start = of_get_property(node, "ranges", &ranges_len);
  1000. if (*ranges_start == NULL) {
  1001. *addr_cells = *c_addr_cells = *c_size_cells = *cell_count = 0;
  1002. *ranges_start = *ranges_end = NULL;
  1003. return 0;
  1004. }
  1005. *ranges_end = *ranges_start + ranges_len / sizeof(__be32);
  1006. *addr_cells = of_n_addr_cells(node);
  1007. prop = of_get_property(node, "#address-cells", NULL);
  1008. *c_addr_cells = be32_to_cpup(prop);
  1009. prop = of_get_property(node, "#size-cells", NULL);
  1010. *c_size_cells = be32_to_cpup(prop);
  1011. *cell_count = *addr_cells + *c_addr_cells + *c_size_cells;
  1012. tuple_len = (*cell_count) * sizeof(__be32);
  1013. if (ranges_len % tuple_len) {
  1014. pr_warn("malformed ranges entry '%pOFn'\n", node);
  1015. return -EINVAL;
  1016. }
  1017. return 0;
  1018. }
  1019. static int __init mbus_dt_setup(struct mvebu_mbus_state *mbus,
  1020. struct device_node *np)
  1021. {
  1022. int addr_cells, c_addr_cells, c_size_cells;
  1023. int i, ret, cell_count;
  1024. const __be32 *r, *ranges_start, *ranges_end;
  1025. ret = mbus_parse_ranges(np, &addr_cells, &c_addr_cells,
  1026. &c_size_cells, &cell_count,
  1027. &ranges_start, &ranges_end);
  1028. if (ret < 0)
  1029. return ret;
  1030. for (i = 0, r = ranges_start; r < ranges_end; r += cell_count, i++) {
  1031. u32 windowid, base, size;
  1032. u8 target, attr;
  1033. /*
  1034. * An entry with a non-zero custom field do not
  1035. * correspond to a static window, so skip it.
  1036. */
  1037. windowid = of_read_number(r, 1);
  1038. if (CUSTOM(windowid))
  1039. continue;
  1040. target = TARGET(windowid);
  1041. attr = ATTR(windowid);
  1042. base = of_read_number(r + c_addr_cells, addr_cells);
  1043. size = of_read_number(r + c_addr_cells + addr_cells,
  1044. c_size_cells);
  1045. ret = mbus_dt_setup_win(mbus, base, size, target, attr);
  1046. if (ret < 0)
  1047. return ret;
  1048. }
  1049. return 0;
  1050. }
  1051. static void __init mvebu_mbus_get_pcie_resources(struct device_node *np,
  1052. struct resource *mem,
  1053. struct resource *io)
  1054. {
  1055. u32 reg[2];
  1056. int ret;
  1057. /*
  1058. * These are optional, so we make sure that resource_size(x) will
  1059. * return 0.
  1060. */
  1061. memset(mem, 0, sizeof(struct resource));
  1062. mem->end = -1;
  1063. memset(io, 0, sizeof(struct resource));
  1064. io->end = -1;
  1065. ret = of_property_read_u32_array(np, "pcie-mem-aperture", reg, ARRAY_SIZE(reg));
  1066. if (!ret) {
  1067. mem->start = reg[0];
  1068. mem->end = mem->start + reg[1] - 1;
  1069. mem->flags = IORESOURCE_MEM;
  1070. }
  1071. ret = of_property_read_u32_array(np, "pcie-io-aperture", reg, ARRAY_SIZE(reg));
  1072. if (!ret) {
  1073. io->start = reg[0];
  1074. io->end = io->start + reg[1] - 1;
  1075. io->flags = IORESOURCE_IO;
  1076. }
  1077. }
  1078. int __init mvebu_mbus_dt_init(bool is_coherent)
  1079. {
  1080. struct resource mbuswins_res, sdramwins_res, mbusbridge_res;
  1081. struct device_node *np, *controller;
  1082. const struct of_device_id *of_id;
  1083. const __be32 *prop;
  1084. int ret;
  1085. np = of_find_matching_node_and_match(NULL, of_mvebu_mbus_ids, &of_id);
  1086. if (!np) {
  1087. pr_err("could not find a matching SoC family\n");
  1088. return -ENODEV;
  1089. }
  1090. mbus_state.soc = of_id->data;
  1091. prop = of_get_property(np, "controller", NULL);
  1092. if (!prop) {
  1093. pr_err("required 'controller' property missing\n");
  1094. return -EINVAL;
  1095. }
  1096. controller = of_find_node_by_phandle(be32_to_cpup(prop));
  1097. if (!controller) {
  1098. pr_err("could not find an 'mbus-controller' node\n");
  1099. return -ENODEV;
  1100. }
  1101. if (of_address_to_resource(controller, 0, &mbuswins_res)) {
  1102. pr_err("cannot get MBUS register address\n");
  1103. return -EINVAL;
  1104. }
  1105. if (of_address_to_resource(controller, 1, &sdramwins_res)) {
  1106. pr_err("cannot get SDRAM register address\n");
  1107. return -EINVAL;
  1108. }
  1109. /*
  1110. * Set the resource to 0 so that it can be left unmapped by
  1111. * mvebu_mbus_common_init() if the DT doesn't carry the
  1112. * necessary information. This is needed to preserve backward
  1113. * compatibility.
  1114. */
  1115. memset(&mbusbridge_res, 0, sizeof(mbusbridge_res));
  1116. if (mbus_state.soc->has_mbus_bridge) {
  1117. if (of_address_to_resource(controller, 2, &mbusbridge_res))
  1118. pr_warn(FW_WARN "deprecated mbus-mvebu Device Tree, suspend/resume will not work\n");
  1119. }
  1120. mbus_state.hw_io_coherency = is_coherent;
  1121. /* Get optional pcie-{mem,io}-aperture properties */
  1122. mvebu_mbus_get_pcie_resources(np, &mbus_state.pcie_mem_aperture,
  1123. &mbus_state.pcie_io_aperture);
  1124. ret = mvebu_mbus_common_init(&mbus_state,
  1125. mbuswins_res.start,
  1126. resource_size(&mbuswins_res),
  1127. sdramwins_res.start,
  1128. resource_size(&sdramwins_res),
  1129. mbusbridge_res.start,
  1130. resource_size(&mbusbridge_res),
  1131. is_coherent);
  1132. if (ret)
  1133. return ret;
  1134. /* Setup statically declared windows in the DT */
  1135. return mbus_dt_setup(&mbus_state, np);
  1136. }
  1137. #endif