mvebu-mbus.c 29 KB

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