pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 <asm/mach/pci.h>
  15. #include "common.h"
  16. /*****************************************************************************
  17. * Orion has one PCIE controller and one PCI controller.
  18. *
  19. * Note1: The local PCIE bus number is '0'. The local PCI bus number
  20. * follows the scanned PCIE bridged busses, if any.
  21. *
  22. * Note2: It is possible for PCI/PCIE agents to access many subsystem's
  23. * space, by configuring BARs and Address Decode Windows, e.g. flashes on
  24. * device bus, Orion registers, etc. However this code only enable the
  25. * access to DDR banks.
  26. ****************************************************************************/
  27. /*****************************************************************************
  28. * PCIE controller
  29. ****************************************************************************/
  30. #define PCIE_CTRL ORION_PCIE_REG(0x1a00)
  31. #define PCIE_STAT ORION_PCIE_REG(0x1a04)
  32. #define PCIE_DEV_ID ORION_PCIE_REG(0x0000)
  33. #define PCIE_CMD_STAT ORION_PCIE_REG(0x0004)
  34. #define PCIE_DEV_REV ORION_PCIE_REG(0x0008)
  35. #define PCIE_MASK ORION_PCIE_REG(0x1910)
  36. #define PCIE_CONF_ADDR ORION_PCIE_REG(0x18f8)
  37. #define PCIE_CONF_DATA ORION_PCIE_REG(0x18fc)
  38. /*
  39. * PCIE_STAT bits
  40. */
  41. #define PCIE_STAT_LINK_DOWN 1
  42. #define PCIE_STAT_BUS_OFFS 8
  43. #define PCIE_STAT_BUS_MASK (0xff << PCIE_STAT_BUS_OFFS)
  44. #define PCIE_STAT_DEV_OFFS 20
  45. #define PCIE_STAT_DEV_MASK (0x1f << PCIE_STAT_DEV_OFFS)
  46. /*
  47. * PCIE_CONF_ADDR bits
  48. */
  49. #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 24) | ((r) & 0xfc))
  50. #define PCIE_CONF_FUNC(f) (((f) & 0x3) << 8)
  51. #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
  52. #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
  53. #define PCIE_CONF_ADDR_EN (1 << 31)
  54. /*
  55. * PCIE config cycles are done by programming the PCIE_CONF_ADDR register
  56. * and then reading the PCIE_CONF_DATA register. Need to make sure these
  57. * transactions are atomic.
  58. */
  59. static DEFINE_SPINLOCK(orion_pcie_lock);
  60. void orion_pcie_id(u32 *dev, u32 *rev)
  61. {
  62. *dev = orion_read(PCIE_DEV_ID) >> 16;
  63. *rev = orion_read(PCIE_DEV_REV) & 0xff;
  64. }
  65. u32 orion_pcie_local_bus_nr(void)
  66. {
  67. u32 stat = orion_read(PCIE_STAT);
  68. return((stat & PCIE_STAT_BUS_MASK) >> PCIE_STAT_BUS_OFFS);
  69. }
  70. static u32 orion_pcie_local_dev_nr(void)
  71. {
  72. u32 stat = orion_read(PCIE_STAT);
  73. return((stat & PCIE_STAT_DEV_MASK) >> PCIE_STAT_DEV_OFFS);
  74. }
  75. static u32 orion_pcie_no_link(void)
  76. {
  77. u32 stat = orion_read(PCIE_STAT);
  78. return(stat & PCIE_STAT_LINK_DOWN);
  79. }
  80. static void orion_pcie_set_bus_nr(int nr)
  81. {
  82. orion_clrbits(PCIE_STAT, PCIE_STAT_BUS_MASK);
  83. orion_setbits(PCIE_STAT, nr << PCIE_STAT_BUS_OFFS);
  84. }
  85. static void orion_pcie_master_slave_enable(void)
  86. {
  87. orion_setbits(PCIE_CMD_STAT, PCI_COMMAND_MASTER |
  88. PCI_COMMAND_IO |
  89. PCI_COMMAND_MEMORY);
  90. }
  91. static void orion_pcie_enable_interrupts(void)
  92. {
  93. /*
  94. * Enable interrupts lines
  95. * INTA[24] INTB[25] INTC[26] INTD[27]
  96. */
  97. orion_setbits(PCIE_MASK, 0xf<<24);
  98. }
  99. static int orion_pcie_valid_config(u32 bus, u32 dev)
  100. {
  101. /*
  102. * Don't go out when trying to access --
  103. * 1. our own device
  104. * 2. where there's no device connected (no link)
  105. * 3. nonexisting devices on local bus
  106. */
  107. if ((orion_pcie_local_bus_nr() == bus) &&
  108. (orion_pcie_local_dev_nr() == dev))
  109. return 0;
  110. if (orion_pcie_no_link())
  111. return 0;
  112. if (bus == orion_pcie_local_bus_nr())
  113. if (((orion_pcie_local_dev_nr() == 0) && (dev != 1)) ||
  114. ((orion_pcie_local_dev_nr() != 0) && (dev != 0)))
  115. return 0;
  116. return 1;
  117. }
  118. static int orion_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  119. int size, u32 *val)
  120. {
  121. unsigned long flags;
  122. unsigned int dev, rev, pcie_addr;
  123. if (orion_pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0) {
  124. *val = 0xffffffff;
  125. return PCIBIOS_DEVICE_NOT_FOUND;
  126. }
  127. spin_lock_irqsave(&orion_pcie_lock, flags);
  128. orion_write(PCIE_CONF_ADDR, PCIE_CONF_BUS(bus->number) |
  129. PCIE_CONF_DEV(PCI_SLOT(devfn)) |
  130. PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
  131. PCIE_CONF_REG(where) | PCIE_CONF_ADDR_EN);
  132. orion_pcie_id(&dev, &rev);
  133. if (dev == MV88F5181_DEV_ID || dev == MV88F5182_DEV_ID) {
  134. /* extended register space */
  135. pcie_addr = ORION_PCIE_WA_VIRT_BASE;
  136. pcie_addr |= PCIE_CONF_BUS(bus->number) |
  137. PCIE_CONF_DEV(PCI_SLOT(devfn)) |
  138. PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
  139. PCIE_CONF_REG(where);
  140. *val = orion_read(pcie_addr);
  141. } else
  142. *val = orion_read(PCIE_CONF_DATA);
  143. if (size == 1)
  144. *val = (*val >> (8*(where & 0x3))) & 0xff;
  145. else if (size == 2)
  146. *val = (*val >> (8*(where & 0x3))) & 0xffff;
  147. spin_unlock_irqrestore(&orion_pcie_lock, flags);
  148. return PCIBIOS_SUCCESSFUL;
  149. }
  150. static int orion_pcie_wr_conf(struct pci_bus *bus, u32 devfn, int where,
  151. int size, u32 val)
  152. {
  153. unsigned long flags;
  154. int ret;
  155. if (orion_pcie_valid_config(bus->number, PCI_SLOT(devfn)) == 0)
  156. return PCIBIOS_DEVICE_NOT_FOUND;
  157. spin_lock_irqsave(&orion_pcie_lock, flags);
  158. ret = PCIBIOS_SUCCESSFUL;
  159. orion_write(PCIE_CONF_ADDR, PCIE_CONF_BUS(bus->number) |
  160. PCIE_CONF_DEV(PCI_SLOT(devfn)) |
  161. PCIE_CONF_FUNC(PCI_FUNC(devfn)) |
  162. PCIE_CONF_REG(where) | PCIE_CONF_ADDR_EN);
  163. if (size == 4) {
  164. __raw_writel(val, PCIE_CONF_DATA);
  165. } else if (size == 2) {
  166. __raw_writew(val, PCIE_CONF_DATA + (where & 0x3));
  167. } else if (size == 1) {
  168. __raw_writeb(val, PCIE_CONF_DATA + (where & 0x3));
  169. } else {
  170. ret = PCIBIOS_BAD_REGISTER_NUMBER;
  171. }
  172. spin_unlock_irqrestore(&orion_pcie_lock, flags);
  173. return ret;
  174. }
  175. struct pci_ops orion_pcie_ops = {
  176. .read = orion_pcie_rd_conf,
  177. .write = orion_pcie_wr_conf,
  178. };
  179. static int orion_pcie_setup(struct pci_sys_data *sys)
  180. {
  181. struct resource *res;
  182. /*
  183. * Master + Slave enable
  184. */
  185. orion_pcie_master_slave_enable();
  186. /*
  187. * Enable interrupts lines A-D
  188. */
  189. orion_pcie_enable_interrupts();
  190. /*
  191. * Request resource
  192. */
  193. res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
  194. if (!res)
  195. panic("orion_pci_setup unable to alloc resources");
  196. /*
  197. * IORESOURCE_IO
  198. */
  199. res[0].name = "PCI-EX I/O Space";
  200. res[0].flags = IORESOURCE_IO;
  201. res[0].start = ORION_PCIE_IO_BUS_BASE;
  202. res[0].end = res[0].start + ORION_PCIE_IO_SIZE - 1;
  203. if (request_resource(&ioport_resource, &res[0]))
  204. panic("Request PCIE IO resource failed\n");
  205. sys->resource[0] = &res[0];
  206. /*
  207. * IORESOURCE_MEM
  208. */
  209. res[1].name = "PCI-EX Memory Space";
  210. res[1].flags = IORESOURCE_MEM;
  211. res[1].start = ORION_PCIE_MEM_PHYS_BASE;
  212. res[1].end = res[1].start + ORION_PCIE_MEM_SIZE - 1;
  213. if (request_resource(&iomem_resource, &res[1]))
  214. panic("Request PCIE Memory resource failed\n");
  215. sys->resource[1] = &res[1];
  216. sys->resource[2] = NULL;
  217. sys->io_offset = 0;
  218. return 1;
  219. }
  220. /*****************************************************************************
  221. * PCI controller
  222. ****************************************************************************/
  223. #define PCI_MODE ORION_PCI_REG(0xd00)
  224. #define PCI_CMD ORION_PCI_REG(0xc00)
  225. #define PCI_P2P_CONF ORION_PCI_REG(0x1d14)
  226. #define PCI_CONF_ADDR ORION_PCI_REG(0xc78)
  227. #define PCI_CONF_DATA ORION_PCI_REG(0xc7c)
  228. /*
  229. * PCI_MODE bits
  230. */
  231. #define PCI_MODE_64BIT (1 << 2)
  232. #define PCI_MODE_PCIX ((1 << 4) | (1 << 5))
  233. /*
  234. * PCI_CMD bits
  235. */
  236. #define PCI_CMD_HOST_REORDER (1 << 29)
  237. /*
  238. * PCI_P2P_CONF bits
  239. */
  240. #define PCI_P2P_BUS_OFFS 16
  241. #define PCI_P2P_BUS_MASK (0xff << PCI_P2P_BUS_OFFS)
  242. #define PCI_P2P_DEV_OFFS 24
  243. #define PCI_P2P_DEV_MASK (0x1f << PCI_P2P_DEV_OFFS)
  244. /*
  245. * PCI_CONF_ADDR bits
  246. */
  247. #define PCI_CONF_REG(reg) ((reg) & 0xfc)
  248. #define PCI_CONF_FUNC(func) (((func) & 0x3) << 8)
  249. #define PCI_CONF_DEV(dev) (((dev) & 0x1f) << 11)
  250. #define PCI_CONF_BUS(bus) (((bus) & 0xff) << 16)
  251. #define PCI_CONF_ADDR_EN (1 << 31)
  252. /*
  253. * Internal configuration space
  254. */
  255. #define PCI_CONF_FUNC_STAT_CMD 0
  256. #define PCI_CONF_REG_STAT_CMD 4
  257. #define PCIX_STAT 0x64
  258. #define PCIX_STAT_BUS_OFFS 8
  259. #define PCIX_STAT_BUS_MASK (0xff << PCIX_STAT_BUS_OFFS)
  260. /*
  261. * PCI config cycles are done by programming the PCI_CONF_ADDR register
  262. * and then reading the PCI_CONF_DATA register. Need to make sure these
  263. * transactions are atomic.
  264. */
  265. static DEFINE_SPINLOCK(orion_pci_lock);
  266. u32 orion_pci_local_bus_nr(void)
  267. {
  268. u32 conf = orion_read(PCI_P2P_CONF);
  269. return((conf & PCI_P2P_BUS_MASK) >> PCI_P2P_BUS_OFFS);
  270. }
  271. u32 orion_pci_local_dev_nr(void)
  272. {
  273. u32 conf = orion_read(PCI_P2P_CONF);
  274. return((conf & PCI_P2P_DEV_MASK) >> PCI_P2P_DEV_OFFS);
  275. }
  276. int orion_pci_hw_rd_conf(u32 bus, u32 dev, u32 func,
  277. u32 where, u32 size, u32 *val)
  278. {
  279. unsigned long flags;
  280. spin_lock_irqsave(&orion_pci_lock, flags);
  281. orion_write(PCI_CONF_ADDR, PCI_CONF_BUS(bus) |
  282. PCI_CONF_DEV(dev) | PCI_CONF_REG(where) |
  283. PCI_CONF_FUNC(func) | PCI_CONF_ADDR_EN);
  284. *val = orion_read(PCI_CONF_DATA);
  285. if (size == 1)
  286. *val = (*val >> (8*(where & 0x3))) & 0xff;
  287. else if (size == 2)
  288. *val = (*val >> (8*(where & 0x3))) & 0xffff;
  289. spin_unlock_irqrestore(&orion_pci_lock, flags);
  290. return PCIBIOS_SUCCESSFUL;
  291. }
  292. int orion_pci_hw_wr_conf(u32 bus, u32 dev, u32 func,
  293. u32 where, u32 size, u32 val)
  294. {
  295. unsigned long flags;
  296. int ret = PCIBIOS_SUCCESSFUL;
  297. spin_lock_irqsave(&orion_pci_lock, flags);
  298. orion_write(PCI_CONF_ADDR, PCI_CONF_BUS(bus) |
  299. PCI_CONF_DEV(dev) | PCI_CONF_REG(where) |
  300. PCI_CONF_FUNC(func) | PCI_CONF_ADDR_EN);
  301. if (size == 4) {
  302. __raw_writel(val, PCI_CONF_DATA);
  303. } else if (size == 2) {
  304. __raw_writew(val, PCI_CONF_DATA + (where & 0x3));
  305. } else if (size == 1) {
  306. __raw_writeb(val, PCI_CONF_DATA + (where & 0x3));
  307. } else {
  308. ret = PCIBIOS_BAD_REGISTER_NUMBER;
  309. }
  310. spin_unlock_irqrestore(&orion_pci_lock, flags);
  311. return ret;
  312. }
  313. static int orion_pci_rd_conf(struct pci_bus *bus, u32 devfn,
  314. int where, int size, u32 *val)
  315. {
  316. /*
  317. * Don't go out for local device
  318. */
  319. if ((orion_pci_local_bus_nr() == bus->number) &&
  320. (orion_pci_local_dev_nr() == PCI_SLOT(devfn))) {
  321. *val = 0xffffffff;
  322. return PCIBIOS_DEVICE_NOT_FOUND;
  323. }
  324. return orion_pci_hw_rd_conf(bus->number, PCI_SLOT(devfn),
  325. PCI_FUNC(devfn), where, size, val);
  326. }
  327. static int orion_pci_wr_conf(struct pci_bus *bus, u32 devfn,
  328. int where, int size, u32 val)
  329. {
  330. /*
  331. * Don't go out for local device
  332. */
  333. if ((orion_pci_local_bus_nr() == bus->number) &&
  334. (orion_pci_local_dev_nr() == PCI_SLOT(devfn)))
  335. return PCIBIOS_DEVICE_NOT_FOUND;
  336. return orion_pci_hw_wr_conf(bus->number, PCI_SLOT(devfn),
  337. PCI_FUNC(devfn), where, size, val);
  338. }
  339. struct pci_ops orion_pci_ops = {
  340. .read = orion_pci_rd_conf,
  341. .write = orion_pci_wr_conf,
  342. };
  343. static void orion_pci_set_bus_nr(int nr)
  344. {
  345. u32 p2p = orion_read(PCI_P2P_CONF);
  346. if (orion_read(PCI_MODE) & PCI_MODE_PCIX) {
  347. /*
  348. * PCI-X mode
  349. */
  350. u32 pcix_status, bus, dev;
  351. bus = (p2p & PCI_P2P_BUS_MASK) >> PCI_P2P_BUS_OFFS;
  352. dev = (p2p & PCI_P2P_DEV_MASK) >> PCI_P2P_DEV_OFFS;
  353. orion_pci_hw_rd_conf(bus, dev, 0, PCIX_STAT, 4, &pcix_status);
  354. pcix_status &= ~PCIX_STAT_BUS_MASK;
  355. pcix_status |= (nr << PCIX_STAT_BUS_OFFS);
  356. orion_pci_hw_wr_conf(bus, dev, 0, PCIX_STAT, 4, pcix_status);
  357. } else {
  358. /*
  359. * PCI Conventional mode
  360. */
  361. p2p &= ~PCI_P2P_BUS_MASK;
  362. p2p |= (nr << PCI_P2P_BUS_OFFS);
  363. orion_write(PCI_P2P_CONF, p2p);
  364. }
  365. }
  366. static void orion_pci_master_slave_enable(void)
  367. {
  368. u32 bus_nr, dev_nr, func, reg, val;
  369. bus_nr = orion_pci_local_bus_nr();
  370. dev_nr = orion_pci_local_dev_nr();
  371. func = PCI_CONF_FUNC_STAT_CMD;
  372. reg = PCI_CONF_REG_STAT_CMD;
  373. orion_pci_hw_rd_conf(bus_nr, dev_nr, func, reg, 4, &val);
  374. val |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  375. orion_pci_hw_wr_conf(bus_nr, dev_nr, func, reg, 4, val | 0x7);
  376. }
  377. static int orion_pci_setup(struct pci_sys_data *sys)
  378. {
  379. struct resource *res;
  380. /*
  381. * Master + Slave enable
  382. */
  383. orion_pci_master_slave_enable();
  384. /*
  385. * Force ordering
  386. */
  387. orion_setbits(PCI_CMD, PCI_CMD_HOST_REORDER);
  388. /*
  389. * Request resources
  390. */
  391. res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
  392. if (!res)
  393. panic("orion_pci_setup unable to alloc resources");
  394. /*
  395. * IORESOURCE_IO
  396. */
  397. res[0].name = "PCI I/O Space";
  398. res[0].flags = IORESOURCE_IO;
  399. res[0].start = ORION_PCI_IO_BUS_BASE;
  400. res[0].end = res[0].start + ORION_PCI_IO_SIZE - 1;
  401. if (request_resource(&ioport_resource, &res[0]))
  402. panic("Request PCI IO resource failed\n");
  403. sys->resource[0] = &res[0];
  404. /*
  405. * IORESOURCE_MEM
  406. */
  407. res[1].name = "PCI Memory Space";
  408. res[1].flags = IORESOURCE_MEM;
  409. res[1].start = ORION_PCI_MEM_PHYS_BASE;
  410. res[1].end = res[1].start + ORION_PCI_MEM_SIZE - 1;
  411. if (request_resource(&iomem_resource, &res[1]))
  412. panic("Request PCI Memory resource failed\n");
  413. sys->resource[1] = &res[1];
  414. sys->resource[2] = NULL;
  415. sys->io_offset = 0;
  416. return 1;
  417. }
  418. /*****************************************************************************
  419. * General PCIE + PCI
  420. ****************************************************************************/
  421. int orion_pci_sys_setup(int nr, struct pci_sys_data *sys)
  422. {
  423. int ret = 0;
  424. if (nr == 0) {
  425. /*
  426. * PCIE setup
  427. */
  428. orion_pcie_set_bus_nr(0);
  429. ret = orion_pcie_setup(sys);
  430. } else if (nr == 1) {
  431. /*
  432. * PCI setup
  433. */
  434. ret = orion_pci_setup(sys);
  435. }
  436. return ret;
  437. }
  438. struct pci_bus *orion_pci_sys_scan_bus(int nr, struct pci_sys_data *sys)
  439. {
  440. struct pci_ops *ops;
  441. struct pci_bus *bus;
  442. if (nr == 0) {
  443. u32 pci_bus;
  444. /*
  445. * PCIE scan
  446. */
  447. ops = &orion_pcie_ops;
  448. bus = pci_scan_bus(sys->busnr, ops, sys);
  449. /*
  450. * Set local PCI bus number to follow PCIE bridges (if any)
  451. */
  452. pci_bus = bus->number + bus->subordinate - bus->secondary + 1;
  453. orion_pci_set_bus_nr(pci_bus);
  454. } else if (nr == 1) {
  455. /*
  456. * PCI scan
  457. */
  458. ops = &orion_pci_ops;
  459. bus = pci_scan_bus(sys->busnr, ops, sys);
  460. } else {
  461. BUG();
  462. bus = NULL;
  463. }
  464. return bus;
  465. }