pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * arch/arm/mach-orion5x/pci.c
  3. *
  4. * PCI and PCIe functions for Marvell Orion System On Chip
  5. *
  6. * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/pci.h>
  14. #include <linux/slab.h>
  15. #include <linux/mbus.h>
  16. #include <asm/irq.h>
  17. #include <asm/mach/pci.h>
  18. #include <plat/pcie.h>
  19. #include "common.h"
  20. /*****************************************************************************
  21. * Orion has one PCIe controller and one PCI controller.
  22. *
  23. * Note1: The local PCIe bus number is '0'. The local PCI bus number
  24. * follows the scanned PCIe bridged busses, if any.
  25. *
  26. * Note2: It is possible for PCI/PCIe agents to access many subsystem's
  27. * space, by configuring BARs and Address Decode Windows, e.g. flashes on
  28. * device bus, Orion registers, etc. However this code only enable the
  29. * access to DDR banks.
  30. ****************************************************************************/
  31. /*****************************************************************************
  32. * PCIe controller
  33. ****************************************************************************/
  34. #define PCIE_BASE ((void __iomem *)ORION5X_PCIE_VIRT_BASE)
  35. void __init orion5x_pcie_id(u32 *dev, u32 *rev)
  36. {
  37. *dev = orion_pcie_dev_id(PCIE_BASE);
  38. *rev = orion_pcie_rev(PCIE_BASE);
  39. }
  40. static int pcie_valid_config(int bus, int dev)
  41. {
  42. /*
  43. * Don't go out when trying to access --
  44. * 1. nonexisting device on local bus
  45. * 2. where there's no device connected (no link)
  46. */
  47. if (bus == 0 && dev == 0)
  48. return 1;
  49. if (!orion_pcie_link_up(PCIE_BASE))
  50. return 0;
  51. if (bus == 0 && dev != 1)
  52. return 0;
  53. return 1;
  54. }
  55. /*
  56. * PCIe config cycles are done by programming the PCIE_CONF_ADDR register
  57. * and then reading the PCIE_CONF_DATA register. Need to make sure these
  58. * transactions are atomic.
  59. */
  60. static DEFINE_SPINLOCK(orion5x_pcie_lock);
  61. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  62. int size, u32 *val)
  63. {
  64. unsigned long flags;
  65. int ret;
  66. if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0) {
  67. *val = 0xffffffff;
  68. return PCIBIOS_DEVICE_NOT_FOUND;
  69. }
  70. spin_lock_irqsave(&orion5x_pcie_lock, flags);
  71. ret = orion_pcie_rd_conf(PCIE_BASE, bus, devfn, where, size, val);
  72. spin_unlock_irqrestore(&orion5x_pcie_lock, flags);
  73. return ret;
  74. }
  75. static int pcie_rd_conf_wa(struct pci_bus *bus, u32 devfn,
  76. int where, int size, u32 *val)
  77. {
  78. int ret;
  79. if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0) {
  80. *val = 0xffffffff;
  81. return PCIBIOS_DEVICE_NOT_FOUND;
  82. }
  83. /*
  84. * We only support access to the non-extended configuration
  85. * space when using the WA access method (or we would have to
  86. * sacrifice 256M of CPU virtual address space.)
  87. */
  88. if (where >= 0x100) {
  89. *val = 0xffffffff;
  90. return PCIBIOS_DEVICE_NOT_FOUND;
  91. }
  92. ret = orion_pcie_rd_conf_wa((void __iomem *)ORION5X_PCIE_WA_VIRT_BASE,
  93. bus, devfn, where, size, val);
  94. return ret;
  95. }
  96. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  97. int where, int size, u32 val)
  98. {
  99. unsigned long flags;
  100. int ret;
  101. if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0)
  102. return PCIBIOS_DEVICE_NOT_FOUND;
  103. spin_lock_irqsave(&orion5x_pcie_lock, flags);
  104. ret = orion_pcie_wr_conf(PCIE_BASE, bus, devfn, where, size, val);
  105. spin_unlock_irqrestore(&orion5x_pcie_lock, flags);
  106. return ret;
  107. }
  108. static struct pci_ops pcie_ops = {
  109. .read = pcie_rd_conf,
  110. .write = pcie_wr_conf,
  111. };
  112. static int __init pcie_setup(struct pci_sys_data *sys)
  113. {
  114. struct resource *res;
  115. int dev;
  116. /*
  117. * Generic PCIe unit setup.
  118. */
  119. orion_pcie_setup(PCIE_BASE, &orion5x_mbus_dram_info);
  120. /*
  121. * Check whether to apply Orion-1/Orion-NAS PCIe config
  122. * read transaction workaround.
  123. */
  124. dev = orion_pcie_dev_id(PCIE_BASE);
  125. if (dev == MV88F5181_DEV_ID || dev == MV88F5182_DEV_ID) {
  126. printk(KERN_NOTICE "Applying Orion-1/Orion-NAS PCIe config "
  127. "read transaction workaround\n");
  128. orion5x_setup_pcie_wa_win(ORION5X_PCIE_WA_PHYS_BASE,
  129. ORION5X_PCIE_WA_SIZE);
  130. pcie_ops.read = pcie_rd_conf_wa;
  131. }
  132. /*
  133. * Request resources.
  134. */
  135. res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
  136. if (!res)
  137. panic("pcie_setup unable to alloc resources");
  138. /*
  139. * IORESOURCE_IO
  140. */
  141. res[0].name = "PCIe I/O Space";
  142. res[0].flags = IORESOURCE_IO;
  143. res[0].start = ORION5X_PCIE_IO_BUS_BASE;
  144. res[0].end = res[0].start + ORION5X_PCIE_IO_SIZE - 1;
  145. if (request_resource(&ioport_resource, &res[0]))
  146. panic("Request PCIe IO resource failed\n");
  147. sys->resource[0] = &res[0];
  148. /*
  149. * IORESOURCE_MEM
  150. */
  151. res[1].name = "PCIe Memory Space";
  152. res[1].flags = IORESOURCE_MEM;
  153. res[1].start = ORION5X_PCIE_MEM_PHYS_BASE;
  154. res[1].end = res[1].start + ORION5X_PCIE_MEM_SIZE - 1;
  155. if (request_resource(&iomem_resource, &res[1]))
  156. panic("Request PCIe Memory resource failed\n");
  157. sys->resource[1] = &res[1];
  158. sys->resource[2] = NULL;
  159. sys->io_offset = 0;
  160. return 1;
  161. }
  162. /*****************************************************************************
  163. * PCI controller
  164. ****************************************************************************/
  165. #define ORION5X_PCI_REG(x) (ORION5X_PCI_VIRT_BASE | (x))
  166. #define PCI_MODE ORION5X_PCI_REG(0xd00)
  167. #define PCI_CMD ORION5X_PCI_REG(0xc00)
  168. #define PCI_P2P_CONF ORION5X_PCI_REG(0x1d14)
  169. #define PCI_CONF_ADDR ORION5X_PCI_REG(0xc78)
  170. #define PCI_CONF_DATA ORION5X_PCI_REG(0xc7c)
  171. /*
  172. * PCI_MODE bits
  173. */
  174. #define PCI_MODE_64BIT (1 << 2)
  175. #define PCI_MODE_PCIX ((1 << 4) | (1 << 5))
  176. /*
  177. * PCI_CMD bits
  178. */
  179. #define PCI_CMD_HOST_REORDER (1 << 29)
  180. /*
  181. * PCI_P2P_CONF bits
  182. */
  183. #define PCI_P2P_BUS_OFFS 16
  184. #define PCI_P2P_BUS_MASK (0xff << PCI_P2P_BUS_OFFS)
  185. #define PCI_P2P_DEV_OFFS 24
  186. #define PCI_P2P_DEV_MASK (0x1f << PCI_P2P_DEV_OFFS)
  187. /*
  188. * PCI_CONF_ADDR bits
  189. */
  190. #define PCI_CONF_REG(reg) ((reg) & 0xfc)
  191. #define PCI_CONF_FUNC(func) (((func) & 0x3) << 8)
  192. #define PCI_CONF_DEV(dev) (((dev) & 0x1f) << 11)
  193. #define PCI_CONF_BUS(bus) (((bus) & 0xff) << 16)
  194. #define PCI_CONF_ADDR_EN (1 << 31)
  195. /*
  196. * Internal configuration space
  197. */
  198. #define PCI_CONF_FUNC_STAT_CMD 0
  199. #define PCI_CONF_REG_STAT_CMD 4
  200. #define PCIX_STAT 0x64
  201. #define PCIX_STAT_BUS_OFFS 8
  202. #define PCIX_STAT_BUS_MASK (0xff << PCIX_STAT_BUS_OFFS)
  203. /*
  204. * PCI Address Decode Windows registers
  205. */
  206. #define PCI_BAR_SIZE_DDR_CS(n) (((n) == 0) ? ORION5X_PCI_REG(0xc08) : \
  207. ((n) == 1) ? ORION5X_PCI_REG(0xd08) : \
  208. ((n) == 2) ? ORION5X_PCI_REG(0xc0c) : \
  209. ((n) == 3) ? ORION5X_PCI_REG(0xd0c) : 0)
  210. #define PCI_BAR_REMAP_DDR_CS(n) (((n) == 0) ? ORION5X_PCI_REG(0xc48) : \
  211. ((n) == 1) ? ORION5X_PCI_REG(0xd48) : \
  212. ((n) == 2) ? ORION5X_PCI_REG(0xc4c) : \
  213. ((n) == 3) ? ORION5X_PCI_REG(0xd4c) : 0)
  214. #define PCI_BAR_ENABLE ORION5X_PCI_REG(0xc3c)
  215. #define PCI_ADDR_DECODE_CTRL ORION5X_PCI_REG(0xd3c)
  216. /*
  217. * PCI configuration helpers for BAR settings
  218. */
  219. #define PCI_CONF_FUNC_BAR_CS(n) ((n) >> 1)
  220. #define PCI_CONF_REG_BAR_LO_CS(n) (((n) & 1) ? 0x18 : 0x10)
  221. #define PCI_CONF_REG_BAR_HI_CS(n) (((n) & 1) ? 0x1c : 0x14)
  222. /*
  223. * PCI config cycles are done by programming the PCI_CONF_ADDR register
  224. * and then reading the PCI_CONF_DATA register. Need to make sure these
  225. * transactions are atomic.
  226. */
  227. static DEFINE_SPINLOCK(orion5x_pci_lock);
  228. static int orion5x_pci_cardbus_mode;
  229. static int orion5x_pci_local_bus_nr(void)
  230. {
  231. u32 conf = readl(PCI_P2P_CONF);
  232. return((conf & PCI_P2P_BUS_MASK) >> PCI_P2P_BUS_OFFS);
  233. }
  234. static int orion5x_pci_hw_rd_conf(int bus, int dev, u32 func,
  235. u32 where, u32 size, u32 *val)
  236. {
  237. unsigned long flags;
  238. spin_lock_irqsave(&orion5x_pci_lock, flags);
  239. writel(PCI_CONF_BUS(bus) |
  240. PCI_CONF_DEV(dev) | PCI_CONF_REG(where) |
  241. PCI_CONF_FUNC(func) | PCI_CONF_ADDR_EN, PCI_CONF_ADDR);
  242. *val = readl(PCI_CONF_DATA);
  243. if (size == 1)
  244. *val = (*val >> (8*(where & 0x3))) & 0xff;
  245. else if (size == 2)
  246. *val = (*val >> (8*(where & 0x3))) & 0xffff;
  247. spin_unlock_irqrestore(&orion5x_pci_lock, flags);
  248. return PCIBIOS_SUCCESSFUL;
  249. }
  250. static int orion5x_pci_hw_wr_conf(int bus, int dev, u32 func,
  251. u32 where, u32 size, u32 val)
  252. {
  253. unsigned long flags;
  254. int ret = PCIBIOS_SUCCESSFUL;
  255. spin_lock_irqsave(&orion5x_pci_lock, flags);
  256. writel(PCI_CONF_BUS(bus) |
  257. PCI_CONF_DEV(dev) | PCI_CONF_REG(where) |
  258. PCI_CONF_FUNC(func) | PCI_CONF_ADDR_EN, PCI_CONF_ADDR);
  259. if (size == 4) {
  260. __raw_writel(val, PCI_CONF_DATA);
  261. } else if (size == 2) {
  262. __raw_writew(val, PCI_CONF_DATA + (where & 0x3));
  263. } else if (size == 1) {
  264. __raw_writeb(val, PCI_CONF_DATA + (where & 0x3));
  265. } else {
  266. ret = PCIBIOS_BAD_REGISTER_NUMBER;
  267. }
  268. spin_unlock_irqrestore(&orion5x_pci_lock, flags);
  269. return ret;
  270. }
  271. static int orion5x_pci_valid_config(int bus, u32 devfn)
  272. {
  273. if (bus == orion5x_pci_local_bus_nr()) {
  274. /*
  275. * Don't go out for local device
  276. */
  277. if (PCI_SLOT(devfn) == 0 && PCI_FUNC(devfn) != 0)
  278. return 0;
  279. /*
  280. * When the PCI signals are directly connected to a
  281. * Cardbus slot, ignore all but device IDs 0 and 1.
  282. */
  283. if (orion5x_pci_cardbus_mode && PCI_SLOT(devfn) > 1)
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. static int orion5x_pci_rd_conf(struct pci_bus *bus, u32 devfn,
  289. int where, int size, u32 *val)
  290. {
  291. if (!orion5x_pci_valid_config(bus->number, devfn)) {
  292. *val = 0xffffffff;
  293. return PCIBIOS_DEVICE_NOT_FOUND;
  294. }
  295. return orion5x_pci_hw_rd_conf(bus->number, PCI_SLOT(devfn),
  296. PCI_FUNC(devfn), where, size, val);
  297. }
  298. static int orion5x_pci_wr_conf(struct pci_bus *bus, u32 devfn,
  299. int where, int size, u32 val)
  300. {
  301. if (!orion5x_pci_valid_config(bus->number, devfn))
  302. return PCIBIOS_DEVICE_NOT_FOUND;
  303. return orion5x_pci_hw_wr_conf(bus->number, PCI_SLOT(devfn),
  304. PCI_FUNC(devfn), where, size, val);
  305. }
  306. static struct pci_ops pci_ops = {
  307. .read = orion5x_pci_rd_conf,
  308. .write = orion5x_pci_wr_conf,
  309. };
  310. static void __init orion5x_pci_set_bus_nr(int nr)
  311. {
  312. u32 p2p = readl(PCI_P2P_CONF);
  313. if (readl(PCI_MODE) & PCI_MODE_PCIX) {
  314. /*
  315. * PCI-X mode
  316. */
  317. u32 pcix_status, bus, dev;
  318. bus = (p2p & PCI_P2P_BUS_MASK) >> PCI_P2P_BUS_OFFS;
  319. dev = (p2p & PCI_P2P_DEV_MASK) >> PCI_P2P_DEV_OFFS;
  320. orion5x_pci_hw_rd_conf(bus, dev, 0, PCIX_STAT, 4, &pcix_status);
  321. pcix_status &= ~PCIX_STAT_BUS_MASK;
  322. pcix_status |= (nr << PCIX_STAT_BUS_OFFS);
  323. orion5x_pci_hw_wr_conf(bus, dev, 0, PCIX_STAT, 4, pcix_status);
  324. } else {
  325. /*
  326. * PCI Conventional mode
  327. */
  328. p2p &= ~PCI_P2P_BUS_MASK;
  329. p2p |= (nr << PCI_P2P_BUS_OFFS);
  330. writel(p2p, PCI_P2P_CONF);
  331. }
  332. }
  333. static void __init orion5x_pci_master_slave_enable(void)
  334. {
  335. int bus_nr, func, reg;
  336. u32 val;
  337. bus_nr = orion5x_pci_local_bus_nr();
  338. func = PCI_CONF_FUNC_STAT_CMD;
  339. reg = PCI_CONF_REG_STAT_CMD;
  340. orion5x_pci_hw_rd_conf(bus_nr, 0, func, reg, 4, &val);
  341. val |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  342. orion5x_pci_hw_wr_conf(bus_nr, 0, func, reg, 4, val | 0x7);
  343. }
  344. static void __init orion5x_setup_pci_wins(struct mbus_dram_target_info *dram)
  345. {
  346. u32 win_enable;
  347. int bus;
  348. int i;
  349. /*
  350. * First, disable windows.
  351. */
  352. win_enable = 0xffffffff;
  353. writel(win_enable, PCI_BAR_ENABLE);
  354. /*
  355. * Setup windows for DDR banks.
  356. */
  357. bus = orion5x_pci_local_bus_nr();
  358. for (i = 0; i < dram->num_cs; i++) {
  359. struct mbus_dram_window *cs = dram->cs + i;
  360. u32 func = PCI_CONF_FUNC_BAR_CS(cs->cs_index);
  361. u32 reg;
  362. u32 val;
  363. /*
  364. * Write DRAM bank base address register.
  365. */
  366. reg = PCI_CONF_REG_BAR_LO_CS(cs->cs_index);
  367. orion5x_pci_hw_rd_conf(bus, 0, func, reg, 4, &val);
  368. val = (cs->base & 0xfffff000) | (val & 0xfff);
  369. orion5x_pci_hw_wr_conf(bus, 0, func, reg, 4, val);
  370. /*
  371. * Write DRAM bank size register.
  372. */
  373. reg = PCI_CONF_REG_BAR_HI_CS(cs->cs_index);
  374. orion5x_pci_hw_wr_conf(bus, 0, func, reg, 4, 0);
  375. writel((cs->size - 1) & 0xfffff000,
  376. PCI_BAR_SIZE_DDR_CS(cs->cs_index));
  377. writel(cs->base & 0xfffff000,
  378. PCI_BAR_REMAP_DDR_CS(cs->cs_index));
  379. /*
  380. * Enable decode window for this chip select.
  381. */
  382. win_enable &= ~(1 << cs->cs_index);
  383. }
  384. /*
  385. * Re-enable decode windows.
  386. */
  387. writel(win_enable, PCI_BAR_ENABLE);
  388. /*
  389. * Disable automatic update of address remapping when writing to BARs.
  390. */
  391. orion5x_setbits(PCI_ADDR_DECODE_CTRL, 1);
  392. }
  393. static int __init pci_setup(struct pci_sys_data *sys)
  394. {
  395. struct resource *res;
  396. /*
  397. * Point PCI unit MBUS decode windows to DRAM space.
  398. */
  399. orion5x_setup_pci_wins(&orion5x_mbus_dram_info);
  400. /*
  401. * Master + Slave enable
  402. */
  403. orion5x_pci_master_slave_enable();
  404. /*
  405. * Force ordering
  406. */
  407. orion5x_setbits(PCI_CMD, PCI_CMD_HOST_REORDER);
  408. /*
  409. * Request resources
  410. */
  411. res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
  412. if (!res)
  413. panic("pci_setup unable to alloc resources");
  414. /*
  415. * IORESOURCE_IO
  416. */
  417. res[0].name = "PCI I/O Space";
  418. res[0].flags = IORESOURCE_IO;
  419. res[0].start = ORION5X_PCI_IO_BUS_BASE;
  420. res[0].end = res[0].start + ORION5X_PCI_IO_SIZE - 1;
  421. if (request_resource(&ioport_resource, &res[0]))
  422. panic("Request PCI IO resource failed\n");
  423. sys->resource[0] = &res[0];
  424. /*
  425. * IORESOURCE_MEM
  426. */
  427. res[1].name = "PCI Memory Space";
  428. res[1].flags = IORESOURCE_MEM;
  429. res[1].start = ORION5X_PCI_MEM_PHYS_BASE;
  430. res[1].end = res[1].start + ORION5X_PCI_MEM_SIZE - 1;
  431. if (request_resource(&iomem_resource, &res[1]))
  432. panic("Request PCI Memory resource failed\n");
  433. sys->resource[1] = &res[1];
  434. sys->resource[2] = NULL;
  435. sys->io_offset = 0;
  436. return 1;
  437. }
  438. /*****************************************************************************
  439. * General PCIe + PCI
  440. ****************************************************************************/
  441. static void __devinit rc_pci_fixup(struct pci_dev *dev)
  442. {
  443. /*
  444. * Prevent enumeration of root complex.
  445. */
  446. if (dev->bus->parent == NULL && dev->devfn == 0) {
  447. int i;
  448. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  449. dev->resource[i].start = 0;
  450. dev->resource[i].end = 0;
  451. dev->resource[i].flags = 0;
  452. }
  453. }
  454. }
  455. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  456. static int orion5x_pci_disabled __initdata;
  457. void __init orion5x_pci_disable(void)
  458. {
  459. orion5x_pci_disabled = 1;
  460. }
  461. void __init orion5x_pci_set_cardbus_mode(void)
  462. {
  463. orion5x_pci_cardbus_mode = 1;
  464. }
  465. int __init orion5x_pci_sys_setup(int nr, struct pci_sys_data *sys)
  466. {
  467. int ret = 0;
  468. if (nr == 0) {
  469. orion_pcie_set_local_bus_nr(PCIE_BASE, sys->busnr);
  470. ret = pcie_setup(sys);
  471. } else if (nr == 1 && !orion5x_pci_disabled) {
  472. orion5x_pci_set_bus_nr(sys->busnr);
  473. ret = pci_setup(sys);
  474. }
  475. return ret;
  476. }
  477. struct pci_bus __init *orion5x_pci_sys_scan_bus(int nr, struct pci_sys_data *sys)
  478. {
  479. struct pci_bus *bus;
  480. if (nr == 0) {
  481. bus = pci_scan_bus(sys->busnr, &pcie_ops, sys);
  482. } else if (nr == 1 && !orion5x_pci_disabled) {
  483. bus = pci_scan_bus(sys->busnr, &pci_ops, sys);
  484. } else {
  485. bus = NULL;
  486. BUG();
  487. }
  488. return bus;
  489. }
  490. int __init orion5x_pci_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  491. {
  492. int bus = dev->bus->number;
  493. /*
  494. * PCIe endpoint?
  495. */
  496. if (orion5x_pci_disabled || bus < orion5x_pci_local_bus_nr())
  497. return IRQ_ORION5X_PCIE0_INT;
  498. return -1;
  499. }