pci-v3-semi.c 27 KB

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