pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * arch/arm/mach-orion/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/mbus.h>
  15. #include <asm/mach/pci.h>
  16. #include <asm/plat-orion/pcie.h>
  17. #include "common.h"
  18. /*****************************************************************************
  19. * Orion has one PCIE controller and one PCI controller.
  20. *
  21. * Note1: The local PCIE bus number is '0'. The local PCI bus number
  22. * follows the scanned PCIE bridged busses, if any.
  23. *
  24. * Note2: It is possible for PCI/PCIE agents to access many subsystem's
  25. * space, by configuring BARs and Address Decode Windows, e.g. flashes on
  26. * device bus, Orion registers, etc. However this code only enable the
  27. * access to DDR banks.
  28. ****************************************************************************/
  29. /*****************************************************************************
  30. * PCIE controller
  31. ****************************************************************************/
  32. #define PCIE_BASE ((void __iomem *)ORION_PCIE_VIRT_BASE)
  33. void __init orion_pcie_id(u32 *dev, u32 *rev)
  34. {
  35. *dev = orion_pcie_dev_id(PCIE_BASE);
  36. *rev = orion_pcie_rev(PCIE_BASE);
  37. }
  38. int orion_pcie_local_bus_nr(void)
  39. {
  40. return orion_pcie_get_local_bus_nr(PCIE_BASE);
  41. }
  42. static int pcie_valid_config(int bus, int dev)
  43. {
  44. /*
  45. * Don't go out when trying to access --
  46. * 1. nonexisting device on local bus
  47. * 2. where there's no device connected (no link)
  48. */
  49. if (bus == 0 && dev == 0)
  50. return 1;
  51. if (!orion_pcie_link_up(PCIE_BASE))
  52. return 0;
  53. if (bus == 0 && dev != 1)
  54. return 0;
  55. return 1;
  56. }
  57. /*
  58. * PCIE config cycles are done by programming the PCIE_CONF_ADDR register
  59. * and then reading the PCIE_CONF_DATA register. Need to make sure these
  60. * transactions are atomic.
  61. */
  62. static DEFINE_SPINLOCK(orion_pcie_lock);
  63. static int pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  64. int size, u32 *val)
  65. {
  66. unsigned long flags;
  67. int ret;
  68. if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0) {
  69. *val = 0xffffffff;
  70. return PCIBIOS_DEVICE_NOT_FOUND;
  71. }
  72. spin_lock_irqsave(&orion_pcie_lock, flags);
  73. ret = orion_pcie_rd_conf(PCIE_BASE, bus, devfn, where, size, val);
  74. spin_unlock_irqrestore(&orion_pcie_lock, flags);
  75. return ret;
  76. }
  77. static int pcie_rd_conf_wa(struct pci_bus *bus, u32 devfn,
  78. int where, int size, u32 *val)
  79. {
  80. int ret;
  81. if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0) {
  82. *val = 0xffffffff;
  83. return PCIBIOS_DEVICE_NOT_FOUND;
  84. }
  85. /*
  86. * We only support access to the non-extended configuration
  87. * space when using the WA access method (or we would have to
  88. * sacrifice 256M of CPU virtual address space.)
  89. */
  90. if (where >= 0x100) {
  91. *val = 0xffffffff;
  92. return PCIBIOS_DEVICE_NOT_FOUND;
  93. }
  94. ret = orion_pcie_rd_conf_wa((void __iomem *)ORION_PCIE_WA_VIRT_BASE,
  95. bus, devfn, where, size, val);
  96. return ret;
  97. }
  98. static int pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  99. int where, int size, u32 val)
  100. {
  101. unsigned long flags;
  102. int ret;
  103. if (pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0)
  104. return PCIBIOS_DEVICE_NOT_FOUND;
  105. spin_lock_irqsave(&orion_pcie_lock, flags);
  106. ret = orion_pcie_wr_conf(PCIE_BASE, bus, devfn, where, size, val);
  107. spin_unlock_irqrestore(&orion_pcie_lock, flags);
  108. return ret;
  109. }
  110. struct pci_ops pcie_ops = {
  111. .read = pcie_rd_conf,
  112. .write = pcie_wr_conf,
  113. };
  114. static int __init pcie_setup(struct pci_sys_data *sys)
  115. {
  116. struct resource *res;
  117. int dev;
  118. /*
  119. * Generic PCIe unit setup.
  120. */
  121. orion_pcie_setup(PCIE_BASE, &orion_mbus_dram_info);
  122. /*
  123. * Check whether to apply Orion-1/Orion-NAS PCIe config
  124. * read transaction workaround.
  125. */
  126. dev = orion_pcie_dev_id(PCIE_BASE);
  127. if (dev == MV88F5181_DEV_ID || dev == MV88F5182_DEV_ID) {
  128. printk(KERN_NOTICE "Applying Orion-1/Orion-NAS PCIe config "
  129. "read transaction workaround\n");
  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 = "PCI-EX I/O Space";
  142. res[0].flags = IORESOURCE_IO;
  143. res[0].start = ORION_PCIE_IO_BUS_BASE;
  144. res[0].end = res[0].start + ORION_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 = "PCI-EX Memory Space";
  152. res[1].flags = IORESOURCE_MEM;
  153. res[1].start = ORION_PCIE_MEM_PHYS_BASE;
  154. res[1].end = res[1].start + ORION_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 PCI_MODE ORION_PCI_REG(0xd00)
  166. #define PCI_CMD ORION_PCI_REG(0xc00)
  167. #define PCI_P2P_CONF ORION_PCI_REG(0x1d14)
  168. #define PCI_CONF_ADDR ORION_PCI_REG(0xc78)
  169. #define PCI_CONF_DATA ORION_PCI_REG(0xc7c)
  170. /*
  171. * PCI_MODE bits
  172. */
  173. #define PCI_MODE_64BIT (1 << 2)
  174. #define PCI_MODE_PCIX ((1 << 4) | (1 << 5))
  175. /*
  176. * PCI_CMD bits
  177. */
  178. #define PCI_CMD_HOST_REORDER (1 << 29)
  179. /*
  180. * PCI_P2P_CONF bits
  181. */
  182. #define PCI_P2P_BUS_OFFS 16
  183. #define PCI_P2P_BUS_MASK (0xff << PCI_P2P_BUS_OFFS)
  184. #define PCI_P2P_DEV_OFFS 24
  185. #define PCI_P2P_DEV_MASK (0x1f << PCI_P2P_DEV_OFFS)
  186. /*
  187. * PCI_CONF_ADDR bits
  188. */
  189. #define PCI_CONF_REG(reg) ((reg) & 0xfc)
  190. #define PCI_CONF_FUNC(func) (((func) & 0x3) << 8)
  191. #define PCI_CONF_DEV(dev) (((dev) & 0x1f) << 11)
  192. #define PCI_CONF_BUS(bus) (((bus) & 0xff) << 16)
  193. #define PCI_CONF_ADDR_EN (1 << 31)
  194. /*
  195. * Internal configuration space
  196. */
  197. #define PCI_CONF_FUNC_STAT_CMD 0
  198. #define PCI_CONF_REG_STAT_CMD 4
  199. #define PCIX_STAT 0x64
  200. #define PCIX_STAT_BUS_OFFS 8
  201. #define PCIX_STAT_BUS_MASK (0xff << PCIX_STAT_BUS_OFFS)
  202. /*
  203. * PCI Address Decode Windows registers
  204. */
  205. #define PCI_BAR_SIZE_DDR_CS(n) (((n) == 0) ? ORION_PCI_REG(0xc08) : \
  206. ((n) == 1) ? ORION_PCI_REG(0xd08) : \
  207. ((n) == 2) ? ORION_PCI_REG(0xc0c) : \
  208. ((n) == 3) ? ORION_PCI_REG(0xd0c) : 0)
  209. #define PCI_BAR_REMAP_DDR_CS(n) (((n) ==0) ? ORION_PCI_REG(0xc48) : \
  210. ((n) == 1) ? ORION_PCI_REG(0xd48) : \
  211. ((n) == 2) ? ORION_PCI_REG(0xc4c) : \
  212. ((n) == 3) ? ORION_PCI_REG(0xd4c) : 0)
  213. #define PCI_BAR_ENABLE ORION_PCI_REG(0xc3c)
  214. #define PCI_ADDR_DECODE_CTRL ORION_PCI_REG(0xd3c)
  215. /*
  216. * PCI configuration helpers for BAR settings
  217. */
  218. #define PCI_CONF_FUNC_BAR_CS(n) ((n) >> 1)
  219. #define PCI_CONF_REG_BAR_LO_CS(n) (((n) & 1) ? 0x18 : 0x10)
  220. #define PCI_CONF_REG_BAR_HI_CS(n) (((n) & 1) ? 0x1c : 0x14)
  221. /*
  222. * PCI config cycles are done by programming the PCI_CONF_ADDR register
  223. * and then reading the PCI_CONF_DATA register. Need to make sure these
  224. * transactions are atomic.
  225. */
  226. static DEFINE_SPINLOCK(orion_pci_lock);
  227. int orion_pci_local_bus_nr(void)
  228. {
  229. u32 conf = orion_read(PCI_P2P_CONF);
  230. return((conf & PCI_P2P_BUS_MASK) >> PCI_P2P_BUS_OFFS);
  231. }
  232. static int orion_pci_hw_rd_conf(int bus, int dev, u32 func,
  233. u32 where, u32 size, u32 *val)
  234. {
  235. unsigned long flags;
  236. spin_lock_irqsave(&orion_pci_lock, flags);
  237. orion_write(PCI_CONF_ADDR, PCI_CONF_BUS(bus) |
  238. PCI_CONF_DEV(dev) | PCI_CONF_REG(where) |
  239. PCI_CONF_FUNC(func) | PCI_CONF_ADDR_EN);
  240. *val = orion_read(PCI_CONF_DATA);
  241. if (size == 1)
  242. *val = (*val >> (8*(where & 0x3))) & 0xff;
  243. else if (size == 2)
  244. *val = (*val >> (8*(where & 0x3))) & 0xffff;
  245. spin_unlock_irqrestore(&orion_pci_lock, flags);
  246. return PCIBIOS_SUCCESSFUL;
  247. }
  248. static int orion_pci_hw_wr_conf(int bus, int dev, u32 func,
  249. u32 where, u32 size, u32 val)
  250. {
  251. unsigned long flags;
  252. int ret = PCIBIOS_SUCCESSFUL;
  253. spin_lock_irqsave(&orion_pci_lock, flags);
  254. orion_write(PCI_CONF_ADDR, PCI_CONF_BUS(bus) |
  255. PCI_CONF_DEV(dev) | PCI_CONF_REG(where) |
  256. PCI_CONF_FUNC(func) | PCI_CONF_ADDR_EN);
  257. if (size == 4) {
  258. __raw_writel(val, PCI_CONF_DATA);
  259. } else if (size == 2) {
  260. __raw_writew(val, PCI_CONF_DATA + (where & 0x3));
  261. } else if (size == 1) {
  262. __raw_writeb(val, PCI_CONF_DATA + (where & 0x3));
  263. } else {
  264. ret = PCIBIOS_BAD_REGISTER_NUMBER;
  265. }
  266. spin_unlock_irqrestore(&orion_pci_lock, flags);
  267. return ret;
  268. }
  269. static int orion_pci_rd_conf(struct pci_bus *bus, u32 devfn,
  270. int where, int size, u32 *val)
  271. {
  272. /*
  273. * Don't go out for local device
  274. */
  275. if (bus->number == orion_pci_local_bus_nr() &&
  276. PCI_SLOT(devfn) == 0 && PCI_FUNC(devfn) != 0) {
  277. *val = 0xffffffff;
  278. return PCIBIOS_DEVICE_NOT_FOUND;
  279. }
  280. return orion_pci_hw_rd_conf(bus->number, PCI_SLOT(devfn),
  281. PCI_FUNC(devfn), where, size, val);
  282. }
  283. static int orion_pci_wr_conf(struct pci_bus *bus, u32 devfn,
  284. int where, int size, u32 val)
  285. {
  286. if (bus->number == orion_pci_local_bus_nr() &&
  287. PCI_SLOT(devfn) == 0 && PCI_FUNC(devfn) != 0)
  288. return PCIBIOS_DEVICE_NOT_FOUND;
  289. return orion_pci_hw_wr_conf(bus->number, PCI_SLOT(devfn),
  290. PCI_FUNC(devfn), where, size, val);
  291. }
  292. struct pci_ops pci_ops = {
  293. .read = orion_pci_rd_conf,
  294. .write = orion_pci_wr_conf,
  295. };
  296. static void __init orion_pci_set_bus_nr(int nr)
  297. {
  298. u32 p2p = orion_read(PCI_P2P_CONF);
  299. if (orion_read(PCI_MODE) & PCI_MODE_PCIX) {
  300. /*
  301. * PCI-X mode
  302. */
  303. u32 pcix_status, bus, dev;
  304. bus = (p2p & PCI_P2P_BUS_MASK) >> PCI_P2P_BUS_OFFS;
  305. dev = (p2p & PCI_P2P_DEV_MASK) >> PCI_P2P_DEV_OFFS;
  306. orion_pci_hw_rd_conf(bus, dev, 0, PCIX_STAT, 4, &pcix_status);
  307. pcix_status &= ~PCIX_STAT_BUS_MASK;
  308. pcix_status |= (nr << PCIX_STAT_BUS_OFFS);
  309. orion_pci_hw_wr_conf(bus, dev, 0, PCIX_STAT, 4, pcix_status);
  310. } else {
  311. /*
  312. * PCI Conventional mode
  313. */
  314. p2p &= ~PCI_P2P_BUS_MASK;
  315. p2p |= (nr << PCI_P2P_BUS_OFFS);
  316. orion_write(PCI_P2P_CONF, p2p);
  317. }
  318. }
  319. static void __init orion_pci_master_slave_enable(void)
  320. {
  321. int bus_nr, func, reg;
  322. u32 val;
  323. bus_nr = orion_pci_local_bus_nr();
  324. func = PCI_CONF_FUNC_STAT_CMD;
  325. reg = PCI_CONF_REG_STAT_CMD;
  326. orion_pci_hw_rd_conf(bus_nr, 0, func, reg, 4, &val);
  327. val |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  328. orion_pci_hw_wr_conf(bus_nr, 0, func, reg, 4, val | 0x7);
  329. }
  330. static void __init orion_setup_pci_wins(struct mbus_dram_target_info *dram)
  331. {
  332. u32 win_enable;
  333. int bus;
  334. int i;
  335. /*
  336. * First, disable windows.
  337. */
  338. win_enable = 0xffffffff;
  339. orion_write(PCI_BAR_ENABLE, win_enable);
  340. /*
  341. * Setup windows for DDR banks.
  342. */
  343. bus = orion_pci_local_bus_nr();
  344. for (i = 0; i < dram->num_cs; i++) {
  345. struct mbus_dram_window *cs = dram->cs + i;
  346. u32 func = PCI_CONF_FUNC_BAR_CS(cs->cs_index);
  347. u32 reg;
  348. u32 val;
  349. /*
  350. * Write DRAM bank base address register.
  351. */
  352. reg = PCI_CONF_REG_BAR_LO_CS(cs->cs_index);
  353. orion_pci_hw_rd_conf(bus, 0, func, reg, 4, &val);
  354. val = (cs->base & 0xfffff000) | (val & 0xfff);
  355. orion_pci_hw_wr_conf(bus, 0, func, reg, 4, val);
  356. /*
  357. * Write DRAM bank size register.
  358. */
  359. reg = PCI_CONF_REG_BAR_HI_CS(cs->cs_index);
  360. orion_pci_hw_wr_conf(bus, 0, func, reg, 4, 0);
  361. orion_write(PCI_BAR_SIZE_DDR_CS(cs->cs_index),
  362. (cs->size - 1) & 0xfffff000);
  363. orion_write(PCI_BAR_REMAP_DDR_CS(cs->cs_index),
  364. cs->base & 0xfffff000);
  365. /*
  366. * Enable decode window for this chip select.
  367. */
  368. win_enable &= ~(1 << cs->cs_index);
  369. }
  370. /*
  371. * Re-enable decode windows.
  372. */
  373. orion_write(PCI_BAR_ENABLE, win_enable);
  374. /*
  375. * Disable automatic update of address remaping when writing to BARs.
  376. */
  377. orion_setbits(PCI_ADDR_DECODE_CTRL, 1);
  378. }
  379. static int __init pci_setup(struct pci_sys_data *sys)
  380. {
  381. struct resource *res;
  382. /*
  383. * Point PCI unit MBUS decode windows to DRAM space.
  384. */
  385. orion_setup_pci_wins(&orion_mbus_dram_info);
  386. /*
  387. * Master + Slave enable
  388. */
  389. orion_pci_master_slave_enable();
  390. /*
  391. * Force ordering
  392. */
  393. orion_setbits(PCI_CMD, PCI_CMD_HOST_REORDER);
  394. /*
  395. * Request resources
  396. */
  397. res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
  398. if (!res)
  399. panic("pci_setup unable to alloc resources");
  400. /*
  401. * IORESOURCE_IO
  402. */
  403. res[0].name = "PCI I/O Space";
  404. res[0].flags = IORESOURCE_IO;
  405. res[0].start = ORION_PCI_IO_BUS_BASE;
  406. res[0].end = res[0].start + ORION_PCI_IO_SIZE - 1;
  407. if (request_resource(&ioport_resource, &res[0]))
  408. panic("Request PCI IO resource failed\n");
  409. sys->resource[0] = &res[0];
  410. /*
  411. * IORESOURCE_MEM
  412. */
  413. res[1].name = "PCI Memory Space";
  414. res[1].flags = IORESOURCE_MEM;
  415. res[1].start = ORION_PCI_MEM_PHYS_BASE;
  416. res[1].end = res[1].start + ORION_PCI_MEM_SIZE - 1;
  417. if (request_resource(&iomem_resource, &res[1]))
  418. panic("Request PCI Memory resource failed\n");
  419. sys->resource[1] = &res[1];
  420. sys->resource[2] = NULL;
  421. sys->io_offset = 0;
  422. return 1;
  423. }
  424. /*****************************************************************************
  425. * General PCIE + PCI
  426. ****************************************************************************/
  427. static void __devinit rc_pci_fixup(struct pci_dev *dev)
  428. {
  429. /*
  430. * Prevent enumeration of root complex.
  431. */
  432. if (dev->bus->parent == NULL && dev->devfn == 0) {
  433. int i;
  434. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  435. dev->resource[i].start = 0;
  436. dev->resource[i].end = 0;
  437. dev->resource[i].flags = 0;
  438. }
  439. }
  440. }
  441. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_MARVELL, PCI_ANY_ID, rc_pci_fixup);
  442. int __init orion_pci_sys_setup(int nr, struct pci_sys_data *sys)
  443. {
  444. int ret = 0;
  445. if (nr == 0) {
  446. orion_pcie_set_local_bus_nr(PCIE_BASE, sys->busnr);
  447. ret = pcie_setup(sys);
  448. } else if (nr == 1) {
  449. orion_pci_set_bus_nr(sys->busnr);
  450. ret = pci_setup(sys);
  451. }
  452. return ret;
  453. }
  454. struct pci_bus __init *orion_pci_sys_scan_bus(int nr, struct pci_sys_data *sys)
  455. {
  456. struct pci_bus *bus;
  457. if (nr == 0) {
  458. bus = pci_scan_bus(sys->busnr, &pcie_ops, sys);
  459. } else if (nr == 1) {
  460. bus = pci_scan_bus(sys->busnr, &pci_ops, sys);
  461. } else {
  462. bus = NULL;
  463. BUG();
  464. }
  465. return bus;
  466. }