pci-v3-semi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for V3 Semiconductor PCI Local Bus to PCI Bridge
  4. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  5. *
  6. * Based on the code from arch/arm/mach-integrator/pci_v3.c
  7. * Copyright (C) 1999 ARM Limited
  8. * Copyright (C) 2000-2001 Deep Blue Solutions Ltd
  9. *
  10. * Contributors to the old driver include:
  11. * Russell King <linux@armlinux.org.uk>
  12. * David A. Rusling <david.rusling@linaro.org> (uHAL, ARM Firmware suite)
  13. * Rob Herring <robh@kernel.org>
  14. * Liviu Dudau <Liviu.Dudau@arm.com>
  15. * Grant Likely <grant.likely@secretlab.ca>
  16. * Arnd Bergmann <arnd@arndb.de>
  17. * Bjorn Helgaas <bhelgaas@google.com>
  18. */
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/of_pci.h>
  27. #include <linux/pci.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/bitops.h>
  31. #include <linux/irq.h>
  32. #include <linux/mfd/syscon.h>
  33. #include <linux/regmap.h>
  34. #include <linux/clk.h>
  35. #define V3_PCI_VENDOR 0x00000000
  36. #define V3_PCI_DEVICE 0x00000002
  37. #define V3_PCI_CMD 0x00000004
  38. #define V3_PCI_STAT 0x00000006
  39. #define V3_PCI_CC_REV 0x00000008
  40. #define V3_PCI_HDR_CFG 0x0000000C
  41. #define V3_PCI_IO_BASE 0x00000010
  42. #define V3_PCI_BASE0 0x00000014
  43. #define V3_PCI_BASE1 0x00000018
  44. #define V3_PCI_SUB_VENDOR 0x0000002C
  45. #define V3_PCI_SUB_ID 0x0000002E
  46. #define V3_PCI_ROM 0x00000030
  47. #define V3_PCI_BPARAM 0x0000003C
  48. #define V3_PCI_MAP0 0x00000040
  49. #define V3_PCI_MAP1 0x00000044
  50. #define V3_PCI_INT_STAT 0x00000048
  51. #define V3_PCI_INT_CFG 0x0000004C
  52. #define V3_LB_BASE0 0x00000054
  53. #define V3_LB_BASE1 0x00000058
  54. #define V3_LB_MAP0 0x0000005E
  55. #define V3_LB_MAP1 0x00000062
  56. #define V3_LB_BASE2 0x00000064
  57. #define V3_LB_MAP2 0x00000066
  58. #define V3_LB_SIZE 0x00000068
  59. #define V3_LB_IO_BASE 0x0000006E
  60. #define V3_FIFO_CFG 0x00000070
  61. #define V3_FIFO_PRIORITY 0x00000072
  62. #define V3_FIFO_STAT 0x00000074
  63. #define V3_LB_ISTAT 0x00000076
  64. #define V3_LB_IMASK 0x00000077
  65. #define V3_SYSTEM 0x00000078
  66. #define V3_LB_CFG 0x0000007A
  67. #define V3_PCI_CFG 0x0000007C
  68. #define V3_DMA_PCI_ADR0 0x00000080
  69. #define V3_DMA_PCI_ADR1 0x00000090
  70. #define V3_DMA_LOCAL_ADR0 0x00000084
  71. #define V3_DMA_LOCAL_ADR1 0x00000094
  72. #define V3_DMA_LENGTH0 0x00000088
  73. #define V3_DMA_LENGTH1 0x00000098
  74. #define V3_DMA_CSR0 0x0000008B
  75. #define V3_DMA_CSR1 0x0000009B
  76. #define V3_DMA_CTLB_ADR0 0x0000008C
  77. #define V3_DMA_CTLB_ADR1 0x0000009C
  78. #define V3_DMA_DELAY 0x000000E0
  79. #define V3_MAIL_DATA 0x000000C0
  80. #define V3_PCI_MAIL_IEWR 0x000000D0
  81. #define V3_PCI_MAIL_IERD 0x000000D2
  82. #define V3_LB_MAIL_IEWR 0x000000D4
  83. #define V3_LB_MAIL_IERD 0x000000D6
  84. #define V3_MAIL_WR_STAT 0x000000D8
  85. #define V3_MAIL_RD_STAT 0x000000DA
  86. #define V3_QBA_MAP 0x000000DC
  87. /* PCI STATUS bits */
  88. #define V3_PCI_STAT_PAR_ERR BIT(15)
  89. #define V3_PCI_STAT_SYS_ERR BIT(14)
  90. #define V3_PCI_STAT_M_ABORT_ERR BIT(13)
  91. #define V3_PCI_STAT_T_ABORT_ERR BIT(12)
  92. /* LB ISTAT bits */
  93. #define V3_LB_ISTAT_MAILBOX BIT(7)
  94. #define V3_LB_ISTAT_PCI_RD BIT(6)
  95. #define V3_LB_ISTAT_PCI_WR BIT(5)
  96. #define V3_LB_ISTAT_PCI_INT BIT(4)
  97. #define V3_LB_ISTAT_PCI_PERR BIT(3)
  98. #define V3_LB_ISTAT_I2O_QWR BIT(2)
  99. #define V3_LB_ISTAT_DMA1 BIT(1)
  100. #define V3_LB_ISTAT_DMA0 BIT(0)
  101. /* PCI COMMAND bits */
  102. #define V3_COMMAND_M_FBB_EN BIT(9)
  103. #define V3_COMMAND_M_SERR_EN BIT(8)
  104. #define V3_COMMAND_M_PAR_EN BIT(6)
  105. #define V3_COMMAND_M_MASTER_EN BIT(2)
  106. #define V3_COMMAND_M_MEM_EN BIT(1)
  107. #define V3_COMMAND_M_IO_EN BIT(0)
  108. /* SYSTEM bits */
  109. #define V3_SYSTEM_M_RST_OUT BIT(15)
  110. #define V3_SYSTEM_M_LOCK BIT(14)
  111. #define V3_SYSTEM_UNLOCK 0xa05f
  112. /* PCI CFG bits */
  113. #define V3_PCI_CFG_M_I2O_EN BIT(15)
  114. #define V3_PCI_CFG_M_IO_REG_DIS BIT(14)
  115. #define V3_PCI_CFG_M_IO_DIS BIT(13)
  116. #define V3_PCI_CFG_M_EN3V BIT(12)
  117. #define V3_PCI_CFG_M_RETRY_EN BIT(10)
  118. #define V3_PCI_CFG_M_AD_LOW1 BIT(9)
  119. #define V3_PCI_CFG_M_AD_LOW0 BIT(8)
  120. /*
  121. * This is the value applied to C/BE[3:1], with bit 0 always held 0
  122. * during DMA access.
  123. */
  124. #define V3_PCI_CFG_M_RTYPE_SHIFT 5
  125. #define V3_PCI_CFG_M_WTYPE_SHIFT 1
  126. #define V3_PCI_CFG_TYPE_DEFAULT 0x3
  127. /* PCI BASE bits (PCI -> Local Bus) */
  128. #define V3_PCI_BASE_M_ADR_BASE 0xFFF00000U
  129. #define V3_PCI_BASE_M_ADR_BASEL 0x000FFF00U
  130. #define V3_PCI_BASE_M_PREFETCH BIT(3)
  131. #define V3_PCI_BASE_M_TYPE (3 << 1)
  132. #define V3_PCI_BASE_M_IO BIT(0)
  133. /* PCI MAP bits (PCI -> Local bus) */
  134. #define V3_PCI_MAP_M_MAP_ADR 0xFFF00000U
  135. #define V3_PCI_MAP_M_RD_POST_INH BIT(15)
  136. #define V3_PCI_MAP_M_ROM_SIZE (3 << 10)
  137. #define V3_PCI_MAP_M_SWAP (3 << 8)
  138. #define V3_PCI_MAP_M_ADR_SIZE 0x000000F0U
  139. #define V3_PCI_MAP_M_REG_EN BIT(1)
  140. #define V3_PCI_MAP_M_ENABLE BIT(0)
  141. /* LB_BASE0,1 bits (Local bus -> PCI) */
  142. #define V3_LB_BASE_ADR_BASE 0xfff00000U
  143. #define V3_LB_BASE_SWAP (3 << 8)
  144. #define V3_LB_BASE_ADR_SIZE (15 << 4)
  145. #define V3_LB_BASE_PREFETCH BIT(3)
  146. #define V3_LB_BASE_ENABLE BIT(0)
  147. #define V3_LB_BASE_ADR_SIZE_1MB (0 << 4)
  148. #define V3_LB_BASE_ADR_SIZE_2MB (1 << 4)
  149. #define V3_LB_BASE_ADR_SIZE_4MB (2 << 4)
  150. #define V3_LB_BASE_ADR_SIZE_8MB (3 << 4)
  151. #define V3_LB_BASE_ADR_SIZE_16MB (4 << 4)
  152. #define V3_LB_BASE_ADR_SIZE_32MB (5 << 4)
  153. #define V3_LB_BASE_ADR_SIZE_64MB (6 << 4)
  154. #define V3_LB_BASE_ADR_SIZE_128MB (7 << 4)
  155. #define V3_LB_BASE_ADR_SIZE_256MB (8 << 4)
  156. #define V3_LB_BASE_ADR_SIZE_512MB (9 << 4)
  157. #define V3_LB_BASE_ADR_SIZE_1GB (10 << 4)
  158. #define V3_LB_BASE_ADR_SIZE_2GB (11 << 4)
  159. #define v3_addr_to_lb_base(a) ((a) & V3_LB_BASE_ADR_BASE)
  160. /* LB_MAP0,1 bits (Local bus -> PCI) */
  161. #define V3_LB_MAP_MAP_ADR 0xfff0U
  162. #define V3_LB_MAP_TYPE (7 << 1)
  163. #define V3_LB_MAP_AD_LOW_EN BIT(0)
  164. #define V3_LB_MAP_TYPE_IACK (0 << 1)
  165. #define V3_LB_MAP_TYPE_IO (1 << 1)
  166. #define V3_LB_MAP_TYPE_MEM (3 << 1)
  167. #define V3_LB_MAP_TYPE_CONFIG (5 << 1)
  168. #define V3_LB_MAP_TYPE_MEM_MULTIPLE (6 << 1)
  169. #define v3_addr_to_lb_map(a) (((a) >> 16) & V3_LB_MAP_MAP_ADR)
  170. /* LB_BASE2 bits (Local bus -> PCI IO) */
  171. #define V3_LB_BASE2_ADR_BASE 0xff00U
  172. #define V3_LB_BASE2_SWAP_AUTO (3 << 6)
  173. #define V3_LB_BASE2_ENABLE BIT(0)
  174. #define v3_addr_to_lb_base2(a) (((a) >> 16) & V3_LB_BASE2_ADR_BASE)
  175. /* LB_MAP2 bits (Local bus -> PCI IO) */
  176. #define V3_LB_MAP2_MAP_ADR 0xff00U
  177. #define v3_addr_to_lb_map2(a) (((a) >> 16) & V3_LB_MAP2_MAP_ADR)
  178. /* FIFO priority bits */
  179. #define V3_FIFO_PRIO_LOCAL BIT(12)
  180. #define V3_FIFO_PRIO_LB_RD1_FLUSH_EOB BIT(10)
  181. #define V3_FIFO_PRIO_LB_RD1_FLUSH_AP1 BIT(11)
  182. #define V3_FIFO_PRIO_LB_RD1_FLUSH_ANY (BIT(10)|BIT(11))
  183. #define V3_FIFO_PRIO_LB_RD0_FLUSH_EOB BIT(8)
  184. #define V3_FIFO_PRIO_LB_RD0_FLUSH_AP1 BIT(9)
  185. #define V3_FIFO_PRIO_LB_RD0_FLUSH_ANY (BIT(8)|BIT(9))
  186. #define V3_FIFO_PRIO_PCI BIT(4)
  187. #define V3_FIFO_PRIO_PCI_RD1_FLUSH_EOB BIT(2)
  188. #define V3_FIFO_PRIO_PCI_RD1_FLUSH_AP1 BIT(3)
  189. #define V3_FIFO_PRIO_PCI_RD1_FLUSH_ANY (BIT(2)|BIT(3))
  190. #define V3_FIFO_PRIO_PCI_RD0_FLUSH_EOB BIT(0)
  191. #define V3_FIFO_PRIO_PCI_RD0_FLUSH_AP1 BIT(1)
  192. #define V3_FIFO_PRIO_PCI_RD0_FLUSH_ANY (BIT(0)|BIT(1))
  193. /* Local bus configuration bits */
  194. #define V3_LB_CFG_LB_TO_64_CYCLES 0x0000
  195. #define V3_LB_CFG_LB_TO_256_CYCLES BIT(13)
  196. #define V3_LB_CFG_LB_TO_512_CYCLES BIT(14)
  197. #define V3_LB_CFG_LB_TO_1024_CYCLES (BIT(13)|BIT(14))
  198. #define V3_LB_CFG_LB_RST BIT(12)
  199. #define V3_LB_CFG_LB_PPC_RDY BIT(11)
  200. #define V3_LB_CFG_LB_LB_INT BIT(10)
  201. #define V3_LB_CFG_LB_ERR_EN BIT(9)
  202. #define V3_LB_CFG_LB_RDY_EN BIT(8)
  203. #define V3_LB_CFG_LB_BE_IMODE BIT(7)
  204. #define V3_LB_CFG_LB_BE_OMODE BIT(6)
  205. #define V3_LB_CFG_LB_ENDIAN BIT(5)
  206. #define V3_LB_CFG_LB_PARK_EN BIT(4)
  207. #define V3_LB_CFG_LB_FBB_DIS BIT(2)
  208. /* ARM Integrator-specific extended control registers */
  209. #define INTEGRATOR_SC_PCI_OFFSET 0x18
  210. #define INTEGRATOR_SC_PCI_ENABLE BIT(0)
  211. #define INTEGRATOR_SC_PCI_INTCLR BIT(1)
  212. #define INTEGRATOR_SC_LBFADDR_OFFSET 0x20
  213. #define INTEGRATOR_SC_LBFCODE_OFFSET 0x24
  214. struct v3_pci {
  215. struct device *dev;
  216. void __iomem *base;
  217. void __iomem *config_base;
  218. struct pci_bus *bus;
  219. u32 config_mem;
  220. u32 io_mem;
  221. u32 non_pre_mem;
  222. u32 pre_mem;
  223. phys_addr_t io_bus_addr;
  224. phys_addr_t non_pre_bus_addr;
  225. phys_addr_t pre_bus_addr;
  226. struct regmap *map;
  227. };
  228. /*
  229. * The V3 PCI interface chip in Integrator provides several windows from
  230. * local bus memory into the PCI memory areas. Unfortunately, there
  231. * are not really enough windows for our usage, therefore we reuse
  232. * one of the windows for access to PCI configuration space. On the
  233. * Integrator/AP, the memory map is as follows:
  234. *
  235. * Local Bus Memory Usage
  236. *
  237. * 40000000 - 4FFFFFFF PCI memory. 256M non-prefetchable
  238. * 50000000 - 5FFFFFFF PCI memory. 256M prefetchable
  239. * 60000000 - 60FFFFFF PCI IO. 16M
  240. * 61000000 - 61FFFFFF PCI Configuration. 16M
  241. *
  242. * There are three V3 windows, each described by a pair of V3 registers.
  243. * These are LB_BASE0/LB_MAP0, LB_BASE1/LB_MAP1 and LB_BASE2/LB_MAP2.
  244. * Base0 and Base1 can be used for any type of PCI memory access. Base2
  245. * can be used either for PCI I/O or for I20 accesses. By default, uHAL
  246. * uses this only for PCI IO space.
  247. *
  248. * Normally these spaces are mapped using the following base registers:
  249. *
  250. * Usage Local Bus Memory Base/Map registers used
  251. *
  252. * Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP0
  253. * Mem 50000000 - 5FFFFFFF LB_BASE1/LB_MAP1
  254. * IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP2
  255. * Cfg 61000000 - 61FFFFFF
  256. *
  257. * This means that I20 and PCI configuration space accesses will fail.
  258. * When PCI configuration accesses are needed (via the uHAL PCI
  259. * configuration space primitives) we must remap the spaces as follows:
  260. *
  261. * Usage Local Bus Memory Base/Map registers used
  262. *
  263. * Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP0
  264. * Mem 50000000 - 5FFFFFFF LB_BASE0/LB_MAP0
  265. * IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP2
  266. * Cfg 61000000 - 61FFFFFF LB_BASE1/LB_MAP1
  267. *
  268. * To make this work, the code depends on overlapping windows working.
  269. * The V3 chip translates an address by checking its range within
  270. * each of the BASE/MAP pairs in turn (in ascending register number
  271. * order). It will use the first matching pair. So, for example,
  272. * if the same address is mapped by both LB_BASE0/LB_MAP0 and
  273. * LB_BASE1/LB_MAP1, the V3 will use the translation from
  274. * LB_BASE0/LB_MAP0.
  275. *
  276. * To allow PCI Configuration space access, the code enlarges the
  277. * window mapped by LB_BASE0/LB_MAP0 from 256M to 512M. This occludes
  278. * the windows currently mapped by LB_BASE1/LB_MAP1 so that it can
  279. * be remapped for use by configuration cycles.
  280. *
  281. * At the end of the PCI Configuration space accesses,
  282. * LB_BASE1/LB_MAP1 is reset to map PCI Memory. Finally the window
  283. * mapped by LB_BASE0/LB_MAP0 is reduced in size from 512M to 256M to
  284. * reveal the now restored LB_BASE1/LB_MAP1 window.
  285. *
  286. * NOTE: We do not set up I2O mapping. I suspect that this is only
  287. * for an intelligent (target) device. Using I2O disables most of
  288. * the mappings into PCI memory.
  289. */
  290. static void __iomem *v3_map_bus(struct pci_bus *bus,
  291. unsigned int devfn, int offset)
  292. {
  293. struct v3_pci *v3 = bus->sysdata;
  294. unsigned int address, mapaddress, busnr;
  295. busnr = bus->number;
  296. if (busnr == 0) {
  297. int slot = PCI_SLOT(devfn);
  298. /*
  299. * local bus segment so need a type 0 config cycle
  300. *
  301. * build the PCI configuration "address" with one-hot in
  302. * A31-A11
  303. *
  304. * mapaddress:
  305. * 3:1 = config cycle (101)
  306. * 0 = PCI A1 & A0 are 0 (0)
  307. */
  308. address = PCI_FUNC(devfn) << 8;
  309. mapaddress = V3_LB_MAP_TYPE_CONFIG;
  310. if (slot > 12)
  311. /*
  312. * high order bits are handled by the MAP register
  313. */
  314. mapaddress |= BIT(slot - 5);
  315. else
  316. /*
  317. * low order bits handled directly in the address
  318. */
  319. address |= BIT(slot + 11);
  320. } else {
  321. /*
  322. * not the local bus segment so need a type 1 config cycle
  323. *
  324. * address:
  325. * 23:16 = bus number
  326. * 15:11 = slot number (7:3 of devfn)
  327. * 10:8 = func number (2:0 of devfn)
  328. *
  329. * mapaddress:
  330. * 3:1 = config cycle (101)
  331. * 0 = PCI A1 & A0 from host bus (1)
  332. */
  333. mapaddress = V3_LB_MAP_TYPE_CONFIG | V3_LB_MAP_AD_LOW_EN;
  334. address = (busnr << 16) | (devfn << 8);
  335. }
  336. /*
  337. * Set up base0 to see all 512Mbytes of memory space (not
  338. * prefetchable), this frees up base1 for re-use by
  339. * configuration memory
  340. */
  341. writel(v3_addr_to_lb_base(v3->non_pre_mem) |
  342. V3_LB_BASE_ADR_SIZE_512MB | V3_LB_BASE_ENABLE,
  343. v3->base + V3_LB_BASE0);
  344. /*
  345. * Set up base1/map1 to point into configuration space.
  346. * The config mem is always 16MB.
  347. */
  348. writel(v3_addr_to_lb_base(v3->config_mem) |
  349. V3_LB_BASE_ADR_SIZE_16MB | V3_LB_BASE_ENABLE,
  350. v3->base + V3_LB_BASE1);
  351. writew(mapaddress, v3->base + V3_LB_MAP1);
  352. return v3->config_base + address + offset;
  353. }
  354. static void v3_unmap_bus(struct v3_pci *v3)
  355. {
  356. /*
  357. * Reassign base1 for use by prefetchable PCI memory
  358. */
  359. writel(v3_addr_to_lb_base(v3->pre_mem) |
  360. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
  361. V3_LB_BASE_ENABLE,
  362. v3->base + V3_LB_BASE1);
  363. writew(v3_addr_to_lb_map(v3->pre_bus_addr) |
  364. V3_LB_MAP_TYPE_MEM, /* was V3_LB_MAP_TYPE_MEM_MULTIPLE */
  365. v3->base + V3_LB_MAP1);
  366. /*
  367. * And shrink base0 back to a 256M window (NOTE: MAP0 already correct)
  368. */
  369. writel(v3_addr_to_lb_base(v3->non_pre_mem) |
  370. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE,
  371. v3->base + V3_LB_BASE0);
  372. }
  373. static int v3_pci_read_config(struct pci_bus *bus, unsigned int fn,
  374. int config, int size, u32 *value)
  375. {
  376. struct v3_pci *v3 = bus->sysdata;
  377. int ret;
  378. dev_dbg(&bus->dev,
  379. "[read] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n",
  380. PCI_SLOT(fn), PCI_FUNC(fn), config, size, *value);
  381. ret = pci_generic_config_read(bus, fn, config, size, value);
  382. v3_unmap_bus(v3);
  383. return ret;
  384. }
  385. static int v3_pci_write_config(struct pci_bus *bus, unsigned int fn,
  386. int config, int size, u32 value)
  387. {
  388. struct v3_pci *v3 = bus->sysdata;
  389. int ret;
  390. dev_dbg(&bus->dev,
  391. "[write] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n",
  392. PCI_SLOT(fn), PCI_FUNC(fn), config, size, value);
  393. ret = pci_generic_config_write(bus, fn, config, size, value);
  394. v3_unmap_bus(v3);
  395. return ret;
  396. }
  397. static struct pci_ops v3_pci_ops = {
  398. .map_bus = v3_map_bus,
  399. .read = v3_pci_read_config,
  400. .write = v3_pci_write_config,
  401. };
  402. static irqreturn_t v3_irq(int irq, void *data)
  403. {
  404. struct v3_pci *v3 = data;
  405. struct device *dev = v3->dev;
  406. u32 status;
  407. status = readw(v3->base + V3_PCI_STAT);
  408. if (status & V3_PCI_STAT_PAR_ERR)
  409. dev_err(dev, "parity error interrupt\n");
  410. if (status & V3_PCI_STAT_SYS_ERR)
  411. dev_err(dev, "system error interrupt\n");
  412. if (status & V3_PCI_STAT_M_ABORT_ERR)
  413. dev_err(dev, "master abort error interrupt\n");
  414. if (status & V3_PCI_STAT_T_ABORT_ERR)
  415. dev_err(dev, "target abort error interrupt\n");
  416. writew(status, v3->base + V3_PCI_STAT);
  417. status = readb(v3->base + V3_LB_ISTAT);
  418. if (status & V3_LB_ISTAT_MAILBOX)
  419. dev_info(dev, "PCI mailbox interrupt\n");
  420. if (status & V3_LB_ISTAT_PCI_RD)
  421. dev_err(dev, "PCI target LB->PCI READ abort interrupt\n");
  422. if (status & V3_LB_ISTAT_PCI_WR)
  423. dev_err(dev, "PCI target LB->PCI WRITE abort interrupt\n");
  424. if (status & V3_LB_ISTAT_PCI_INT)
  425. dev_info(dev, "PCI pin interrupt\n");
  426. if (status & V3_LB_ISTAT_PCI_PERR)
  427. dev_err(dev, "PCI parity error interrupt\n");
  428. if (status & V3_LB_ISTAT_I2O_QWR)
  429. dev_info(dev, "I2O inbound post queue interrupt\n");
  430. if (status & V3_LB_ISTAT_DMA1)
  431. dev_info(dev, "DMA channel 1 interrupt\n");
  432. if (status & V3_LB_ISTAT_DMA0)
  433. dev_info(dev, "DMA channel 0 interrupt\n");
  434. /* Clear all possible interrupts on the local bus */
  435. writeb(0, v3->base + V3_LB_ISTAT);
  436. if (v3->map)
  437. regmap_write(v3->map, INTEGRATOR_SC_PCI_OFFSET,
  438. INTEGRATOR_SC_PCI_ENABLE |
  439. INTEGRATOR_SC_PCI_INTCLR);
  440. return IRQ_HANDLED;
  441. }
  442. static int v3_integrator_init(struct v3_pci *v3)
  443. {
  444. unsigned int val;
  445. v3->map =
  446. syscon_regmap_lookup_by_compatible("arm,integrator-ap-syscon");
  447. if (IS_ERR(v3->map)) {
  448. dev_err(v3->dev, "no syscon\n");
  449. return -ENODEV;
  450. }
  451. regmap_read(v3->map, INTEGRATOR_SC_PCI_OFFSET, &val);
  452. /* Take the PCI bridge out of reset, clear IRQs */
  453. regmap_write(v3->map, INTEGRATOR_SC_PCI_OFFSET,
  454. INTEGRATOR_SC_PCI_ENABLE |
  455. INTEGRATOR_SC_PCI_INTCLR);
  456. if (!(val & INTEGRATOR_SC_PCI_ENABLE)) {
  457. /* If we were in reset we need to sleep a bit */
  458. msleep(230);
  459. /* Set the physical base for the controller itself */
  460. writel(0x6200, v3->base + V3_LB_IO_BASE);
  461. /* Wait for the mailbox to settle after reset */
  462. do {
  463. writeb(0xaa, v3->base + V3_MAIL_DATA);
  464. writeb(0x55, v3->base + V3_MAIL_DATA + 4);
  465. } while (readb(v3->base + V3_MAIL_DATA) != 0xaa &&
  466. readb(v3->base + V3_MAIL_DATA) != 0x55);
  467. }
  468. dev_info(v3->dev, "initialized PCI V3 Integrator/AP integration\n");
  469. return 0;
  470. }
  471. static int v3_pci_setup_resource(struct v3_pci *v3,
  472. resource_size_t io_base,
  473. struct pci_host_bridge *host,
  474. struct resource_entry *win)
  475. {
  476. struct device *dev = v3->dev;
  477. struct resource *mem;
  478. struct resource *io;
  479. int ret;
  480. switch (resource_type(win->res)) {
  481. case IORESOURCE_IO:
  482. io = win->res;
  483. io->name = "V3 PCI I/O";
  484. v3->io_mem = io_base;
  485. v3->io_bus_addr = io->start - win->offset;
  486. dev_dbg(dev, "I/O window %pR, bus addr %pap\n",
  487. io, &v3->io_bus_addr);
  488. ret = pci_remap_iospace(io, io_base);
  489. if (ret) {
  490. dev_warn(dev,
  491. "error %d: failed to map resource %pR\n",
  492. ret, io);
  493. return ret;
  494. }
  495. /* Setup window 2 - PCI I/O */
  496. writel(v3_addr_to_lb_base2(v3->io_mem) |
  497. V3_LB_BASE2_ENABLE,
  498. v3->base + V3_LB_BASE2);
  499. writew(v3_addr_to_lb_map2(v3->io_bus_addr),
  500. v3->base + V3_LB_MAP2);
  501. break;
  502. case IORESOURCE_MEM:
  503. mem = win->res;
  504. if (mem->flags & IORESOURCE_PREFETCH) {
  505. mem->name = "V3 PCI PRE-MEM";
  506. v3->pre_mem = mem->start;
  507. v3->pre_bus_addr = mem->start - win->offset;
  508. dev_dbg(dev, "PREFETCHABLE MEM window %pR, bus addr %pap\n",
  509. mem, &v3->pre_bus_addr);
  510. if (resource_size(mem) != SZ_256M) {
  511. dev_err(dev, "prefetchable memory range is not 256MB\n");
  512. return -EINVAL;
  513. }
  514. if (v3->non_pre_mem &&
  515. (mem->start != v3->non_pre_mem + SZ_256M)) {
  516. dev_err(dev,
  517. "prefetchable memory is not adjacent to non-prefetchable memory\n");
  518. return -EINVAL;
  519. }
  520. /* Setup window 1 - PCI prefetchable memory */
  521. writel(v3_addr_to_lb_base(v3->pre_mem) |
  522. V3_LB_BASE_ADR_SIZE_256MB |
  523. V3_LB_BASE_PREFETCH |
  524. V3_LB_BASE_ENABLE,
  525. v3->base + V3_LB_BASE1);
  526. writew(v3_addr_to_lb_map(v3->pre_bus_addr) |
  527. V3_LB_MAP_TYPE_MEM, /* Was V3_LB_MAP_TYPE_MEM_MULTIPLE */
  528. v3->base + V3_LB_MAP1);
  529. } else {
  530. mem->name = "V3 PCI NON-PRE-MEM";
  531. v3->non_pre_mem = mem->start;
  532. v3->non_pre_bus_addr = mem->start - win->offset;
  533. dev_dbg(dev, "NON-PREFETCHABLE MEM window %pR, bus addr %pap\n",
  534. mem, &v3->non_pre_bus_addr);
  535. if (resource_size(mem) != SZ_256M) {
  536. dev_err(dev,
  537. "non-prefetchable memory range is not 256MB\n");
  538. return -EINVAL;
  539. }
  540. /* Setup window 0 - PCI non-prefetchable memory */
  541. writel(v3_addr_to_lb_base(v3->non_pre_mem) |
  542. V3_LB_BASE_ADR_SIZE_256MB |
  543. V3_LB_BASE_ENABLE,
  544. v3->base + V3_LB_BASE0);
  545. writew(v3_addr_to_lb_map(v3->non_pre_bus_addr) |
  546. V3_LB_MAP_TYPE_MEM,
  547. v3->base + V3_LB_MAP0);
  548. }
  549. break;
  550. case IORESOURCE_BUS:
  551. dev_dbg(dev, "BUS %pR\n", win->res);
  552. host->busnr = win->res->start;
  553. break;
  554. default:
  555. dev_info(dev, "Unknown resource type %lu\n",
  556. resource_type(win->res));
  557. break;
  558. }
  559. return 0;
  560. }
  561. static int v3_get_dma_range_config(struct v3_pci *v3,
  562. struct of_pci_range *range,
  563. u32 *pci_base, u32 *pci_map)
  564. {
  565. struct device *dev = v3->dev;
  566. u64 cpu_end = range->cpu_addr + range->size - 1;
  567. u64 pci_end = range->pci_addr + range->size - 1;
  568. u32 val;
  569. if (range->pci_addr & ~V3_PCI_BASE_M_ADR_BASE) {
  570. dev_err(dev, "illegal range, only PCI bits 31..20 allowed\n");
  571. return -EINVAL;
  572. }
  573. val = ((u32)range->pci_addr) & V3_PCI_BASE_M_ADR_BASE;
  574. *pci_base = val;
  575. if (range->cpu_addr & ~V3_PCI_MAP_M_MAP_ADR) {
  576. dev_err(dev, "illegal range, only CPU bits 31..20 allowed\n");
  577. return -EINVAL;
  578. }
  579. val = ((u32)range->cpu_addr) & V3_PCI_MAP_M_MAP_ADR;
  580. switch (range->size) {
  581. case SZ_1M:
  582. val |= V3_LB_BASE_ADR_SIZE_1MB;
  583. break;
  584. case SZ_2M:
  585. val |= V3_LB_BASE_ADR_SIZE_2MB;
  586. break;
  587. case SZ_4M:
  588. val |= V3_LB_BASE_ADR_SIZE_4MB;
  589. break;
  590. case SZ_8M:
  591. val |= V3_LB_BASE_ADR_SIZE_8MB;
  592. break;
  593. case SZ_16M:
  594. val |= V3_LB_BASE_ADR_SIZE_16MB;
  595. break;
  596. case SZ_32M:
  597. val |= V3_LB_BASE_ADR_SIZE_32MB;
  598. break;
  599. case SZ_64M:
  600. val |= V3_LB_BASE_ADR_SIZE_64MB;
  601. break;
  602. case SZ_128M:
  603. val |= V3_LB_BASE_ADR_SIZE_128MB;
  604. break;
  605. case SZ_256M:
  606. val |= V3_LB_BASE_ADR_SIZE_256MB;
  607. break;
  608. case SZ_512M:
  609. val |= V3_LB_BASE_ADR_SIZE_512MB;
  610. break;
  611. case SZ_1G:
  612. val |= V3_LB_BASE_ADR_SIZE_1GB;
  613. break;
  614. case SZ_2G:
  615. val |= V3_LB_BASE_ADR_SIZE_2GB;
  616. break;
  617. default:
  618. dev_err(v3->dev, "illegal dma memory chunk size\n");
  619. return -EINVAL;
  620. break;
  621. }
  622. val |= V3_PCI_MAP_M_REG_EN | V3_PCI_MAP_M_ENABLE;
  623. *pci_map = val;
  624. dev_dbg(dev,
  625. "DMA MEM CPU: 0x%016llx -> 0x%016llx => "
  626. "PCI: 0x%016llx -> 0x%016llx base %08x map %08x\n",
  627. range->cpu_addr, cpu_end,
  628. range->pci_addr, pci_end,
  629. *pci_base, *pci_map);
  630. return 0;
  631. }
  632. static int v3_pci_parse_map_dma_ranges(struct v3_pci *v3,
  633. struct device_node *np)
  634. {
  635. struct of_pci_range range;
  636. struct of_pci_range_parser parser;
  637. struct device *dev = v3->dev;
  638. int i = 0;
  639. if (of_pci_dma_range_parser_init(&parser, np)) {
  640. dev_err(dev, "missing dma-ranges property\n");
  641. return -EINVAL;
  642. }
  643. /*
  644. * Get the dma-ranges from the device tree
  645. */
  646. for_each_of_pci_range(&parser, &range) {
  647. int ret;
  648. u32 pci_base, pci_map;
  649. ret = v3_get_dma_range_config(v3, &range, &pci_base, &pci_map);
  650. if (ret)
  651. return ret;
  652. if (i == 0) {
  653. writel(pci_base, v3->base + V3_PCI_BASE0);
  654. writel(pci_map, v3->base + V3_PCI_MAP0);
  655. } else if (i == 1) {
  656. writel(pci_base, v3->base + V3_PCI_BASE1);
  657. writel(pci_map, v3->base + V3_PCI_MAP1);
  658. } else {
  659. dev_err(dev, "too many ranges, only two supported\n");
  660. dev_err(dev, "range %d ignored\n", i);
  661. }
  662. i++;
  663. }
  664. return 0;
  665. }
  666. static int v3_pci_probe(struct platform_device *pdev)
  667. {
  668. struct device *dev = &pdev->dev;
  669. struct device_node *np = dev->of_node;
  670. resource_size_t io_base;
  671. struct resource *regs;
  672. struct resource_entry *win;
  673. struct v3_pci *v3;
  674. struct pci_host_bridge *host;
  675. struct clk *clk;
  676. u16 val;
  677. int irq;
  678. int ret;
  679. LIST_HEAD(res);
  680. host = pci_alloc_host_bridge(sizeof(*v3));
  681. if (!host)
  682. return -ENOMEM;
  683. host->dev.parent = dev;
  684. host->ops = &v3_pci_ops;
  685. host->busnr = 0;
  686. host->msi = NULL;
  687. host->map_irq = of_irq_parse_and_map_pci;
  688. host->swizzle_irq = pci_common_swizzle;
  689. v3 = pci_host_bridge_priv(host);
  690. host->sysdata = v3;
  691. v3->dev = dev;
  692. /* Get and enable host clock */
  693. clk = devm_clk_get(dev, NULL);
  694. if (IS_ERR(clk)) {
  695. dev_err(dev, "clock not found\n");
  696. return PTR_ERR(clk);
  697. }
  698. ret = clk_prepare_enable(clk);
  699. if (ret) {
  700. dev_err(dev, "unable to enable clock\n");
  701. return ret;
  702. }
  703. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  704. v3->base = devm_ioremap_resource(dev, regs);
  705. if (IS_ERR(v3->base))
  706. return PTR_ERR(v3->base);
  707. /*
  708. * The hardware has a register with the physical base address
  709. * of the V3 controller itself, verify that this is the same
  710. * as the physical memory we've remapped it from.
  711. */
  712. if (readl(v3->base + V3_LB_IO_BASE) != (regs->start >> 16))
  713. dev_err(dev, "V3_LB_IO_BASE = %08x but device is @%pR\n",
  714. readl(v3->base + V3_LB_IO_BASE), regs);
  715. /* Configuration space is 16MB directly mapped */
  716. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  717. if (resource_size(regs) != SZ_16M) {
  718. dev_err(dev, "config mem is not 16MB!\n");
  719. return -EINVAL;
  720. }
  721. v3->config_mem = regs->start;
  722. v3->config_base = devm_ioremap_resource(dev, regs);
  723. if (IS_ERR(v3->config_base))
  724. return PTR_ERR(v3->config_base);
  725. ret = of_pci_get_host_bridge_resources(np, 0, 0xff, &res, &io_base);
  726. if (ret)
  727. return ret;
  728. ret = devm_request_pci_bus_resources(dev, &res);
  729. if (ret)
  730. return ret;
  731. /* Get and request error IRQ resource */
  732. irq = platform_get_irq(pdev, 0);
  733. if (irq <= 0) {
  734. dev_err(dev, "unable to obtain PCIv3 error IRQ\n");
  735. return -ENODEV;
  736. }
  737. ret = devm_request_irq(dev, irq, v3_irq, 0,
  738. "PCIv3 error", v3);
  739. if (ret < 0) {
  740. dev_err(dev,
  741. "unable to request PCIv3 error IRQ %d (%d)\n",
  742. irq, ret);
  743. return ret;
  744. }
  745. /*
  746. * Unlock V3 registers, but only if they were previously locked.
  747. */
  748. if (readw(v3->base + V3_SYSTEM) & V3_SYSTEM_M_LOCK)
  749. writew(V3_SYSTEM_UNLOCK, v3->base + V3_SYSTEM);
  750. /* Disable all slave access while we set up the windows */
  751. val = readw(v3->base + V3_PCI_CMD);
  752. val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  753. writew(val, v3->base + V3_PCI_CMD);
  754. /* Put the PCI bus into reset */
  755. val = readw(v3->base + V3_SYSTEM);
  756. val &= ~V3_SYSTEM_M_RST_OUT;
  757. writew(val, v3->base + V3_SYSTEM);
  758. /* Retry until we're ready */
  759. val = readw(v3->base + V3_PCI_CFG);
  760. val |= V3_PCI_CFG_M_RETRY_EN;
  761. writew(val, v3->base + V3_PCI_CFG);
  762. /* Set up the local bus protocol */
  763. val = readw(v3->base + V3_LB_CFG);
  764. val |= V3_LB_CFG_LB_BE_IMODE; /* Byte enable input */
  765. val |= V3_LB_CFG_LB_BE_OMODE; /* Byte enable output */
  766. val &= ~V3_LB_CFG_LB_ENDIAN; /* Little endian */
  767. val &= ~V3_LB_CFG_LB_PPC_RDY; /* TODO: when using on PPC403Gx, set to 1 */
  768. writew(val, v3->base + V3_LB_CFG);
  769. /* Enable the PCI bus master */
  770. val = readw(v3->base + V3_PCI_CMD);
  771. val |= PCI_COMMAND_MASTER;
  772. writew(val, v3->base + V3_PCI_CMD);
  773. /* Get the I/O and memory ranges from DT */
  774. resource_list_for_each_entry(win, &res) {
  775. ret = v3_pci_setup_resource(v3, io_base, host, win);
  776. if (ret) {
  777. dev_err(dev, "error setting up resources\n");
  778. return ret;
  779. }
  780. }
  781. ret = v3_pci_parse_map_dma_ranges(v3, np);
  782. if (ret)
  783. return ret;
  784. /*
  785. * Disable PCI to host IO cycles, enable I/O buffers @3.3V,
  786. * set AD_LOW0 to 1 if one of the LB_MAP registers choose
  787. * to use this (should be unused).
  788. */
  789. writel(0x00000000, v3->base + V3_PCI_IO_BASE);
  790. val = V3_PCI_CFG_M_IO_REG_DIS | V3_PCI_CFG_M_IO_DIS |
  791. V3_PCI_CFG_M_EN3V | V3_PCI_CFG_M_AD_LOW0;
  792. /*
  793. * DMA read and write from PCI bus commands types
  794. */
  795. val |= V3_PCI_CFG_TYPE_DEFAULT << V3_PCI_CFG_M_RTYPE_SHIFT;
  796. val |= V3_PCI_CFG_TYPE_DEFAULT << V3_PCI_CFG_M_WTYPE_SHIFT;
  797. writew(val, v3->base + V3_PCI_CFG);
  798. /*
  799. * Set the V3 FIFO such that writes have higher priority than
  800. * reads, and local bus write causes local bus read fifo flush
  801. * on aperture 1. Same for PCI.
  802. */
  803. writew(V3_FIFO_PRIO_LB_RD1_FLUSH_AP1 |
  804. V3_FIFO_PRIO_LB_RD0_FLUSH_AP1 |
  805. V3_FIFO_PRIO_PCI_RD1_FLUSH_AP1 |
  806. V3_FIFO_PRIO_PCI_RD0_FLUSH_AP1,
  807. v3->base + V3_FIFO_PRIORITY);
  808. /*
  809. * Clear any error interrupts, and enable parity and write error
  810. * interrupts
  811. */
  812. writeb(0, v3->base + V3_LB_ISTAT);
  813. val = readw(v3->base + V3_LB_CFG);
  814. val |= V3_LB_CFG_LB_LB_INT;
  815. writew(val, v3->base + V3_LB_CFG);
  816. writeb(V3_LB_ISTAT_PCI_WR | V3_LB_ISTAT_PCI_PERR,
  817. v3->base + V3_LB_IMASK);
  818. /* Special Integrator initialization */
  819. if (of_device_is_compatible(np, "arm,integrator-ap-pci")) {
  820. ret = v3_integrator_init(v3);
  821. if (ret)
  822. return ret;
  823. }
  824. /* Post-init: enable PCI memory and invalidate (master already on) */
  825. val = readw(v3->base + V3_PCI_CMD);
  826. val |= PCI_COMMAND_MEMORY | PCI_COMMAND_INVALIDATE;
  827. writew(val, v3->base + V3_PCI_CMD);
  828. /* Clear pending interrupts */
  829. writeb(0, v3->base + V3_LB_ISTAT);
  830. /* Read or write errors and parity errors cause interrupts */
  831. writeb(V3_LB_ISTAT_PCI_RD | V3_LB_ISTAT_PCI_WR | V3_LB_ISTAT_PCI_PERR,
  832. v3->base + V3_LB_IMASK);
  833. /* Take the PCI bus out of reset so devices can initialize */
  834. val = readw(v3->base + V3_SYSTEM);
  835. val |= V3_SYSTEM_M_RST_OUT;
  836. writew(val, v3->base + V3_SYSTEM);
  837. /*
  838. * Re-lock the system register.
  839. */
  840. val = readw(v3->base + V3_SYSTEM);
  841. val |= V3_SYSTEM_M_LOCK;
  842. writew(val, v3->base + V3_SYSTEM);
  843. list_splice_init(&res, &host->windows);
  844. ret = pci_scan_root_bus_bridge(host);
  845. if (ret) {
  846. dev_err(dev, "failed to register host: %d\n", ret);
  847. return ret;
  848. }
  849. v3->bus = host->bus;
  850. pci_bus_assign_resources(v3->bus);
  851. pci_bus_add_devices(v3->bus);
  852. return 0;
  853. }
  854. static const struct of_device_id v3_pci_of_match[] = {
  855. {
  856. .compatible = "v3,v360epc-pci",
  857. },
  858. {},
  859. };
  860. static struct platform_driver v3_pci_driver = {
  861. .driver = {
  862. .name = "pci-v3-semi",
  863. .of_match_table = of_match_ptr(v3_pci_of_match),
  864. .suppress_bind_attrs = true,
  865. },
  866. .probe = v3_pci_probe,
  867. };
  868. builtin_platform_driver(v3_pci_driver);