pci-v3-semi.c 28 KB

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