pci-v3-semi.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 <[email protected]>
  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 <[email protected]>
  12. * David A. Rusling <[email protected]> (uHAL, ARM Firmware suite)
  13. * Rob Herring <[email protected]>
  14. * Liviu Dudau <[email protected]>
  15. * Grant Likely <[email protected]>
  16. * Arnd Bergmann <[email protected]>
  17. * Bjorn Helgaas <[email protected]>
  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. u32 config_mem;
  220. u32 non_pre_mem;
  221. u32 pre_mem;
  222. phys_addr_t non_pre_bus_addr;
  223. phys_addr_t pre_bus_addr;
  224. struct regmap *map;
  225. };
  226. /*
  227. * The V3 PCI interface chip in Integrator provides several windows from
  228. * local bus memory into the PCI memory areas. Unfortunately, there
  229. * are not really enough windows for our usage, therefore we reuse
  230. * one of the windows for access to PCI configuration space. On the
  231. * Integrator/AP, the memory map is as follows:
  232. *
  233. * Local Bus Memory Usage
  234. *
  235. * 40000000 - 4FFFFFFF PCI memory. 256M non-prefetchable
  236. * 50000000 - 5FFFFFFF PCI memory. 256M prefetchable
  237. * 60000000 - 60FFFFFF PCI IO. 16M
  238. * 61000000 - 61FFFFFF PCI Configuration. 16M
  239. *
  240. * There are three V3 windows, each described by a pair of V3 registers.
  241. * These are LB_BASE0/LB_MAP0, LB_BASE1/LB_MAP1 and LB_BASE2/LB_MAP2.
  242. * Base0 and Base1 can be used for any type of PCI memory access. Base2
  243. * can be used either for PCI I/O or for I20 accesses. By default, uHAL
  244. * uses this only for PCI IO space.
  245. *
  246. * Normally these spaces are mapped using the following base registers:
  247. *
  248. * Usage Local Bus Memory Base/Map registers used
  249. *
  250. * Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP0
  251. * Mem 50000000 - 5FFFFFFF LB_BASE1/LB_MAP1
  252. * IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP2
  253. * Cfg 61000000 - 61FFFFFF
  254. *
  255. * This means that I20 and PCI configuration space accesses will fail.
  256. * When PCI configuration accesses are needed (via the uHAL PCI
  257. * configuration space primitives) we must remap the spaces as follows:
  258. *
  259. * Usage Local Bus Memory Base/Map registers used
  260. *
  261. * Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP0
  262. * Mem 50000000 - 5FFFFFFF LB_BASE0/LB_MAP0
  263. * IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP2
  264. * Cfg 61000000 - 61FFFFFF LB_BASE1/LB_MAP1
  265. *
  266. * To make this work, the code depends on overlapping windows working.
  267. * The V3 chip translates an address by checking its range within
  268. * each of the BASE/MAP pairs in turn (in ascending register number
  269. * order). It will use the first matching pair. So, for example,
  270. * if the same address is mapped by both LB_BASE0/LB_MAP0 and
  271. * LB_BASE1/LB_MAP1, the V3 will use the translation from
  272. * LB_BASE0/LB_MAP0.
  273. *
  274. * To allow PCI Configuration space access, the code enlarges the
  275. * window mapped by LB_BASE0/LB_MAP0 from 256M to 512M. This occludes
  276. * the windows currently mapped by LB_BASE1/LB_MAP1 so that it can
  277. * be remapped for use by configuration cycles.
  278. *
  279. * At the end of the PCI Configuration space accesses,
  280. * LB_BASE1/LB_MAP1 is reset to map PCI Memory. Finally the window
  281. * mapped by LB_BASE0/LB_MAP0 is reduced in size from 512M to 256M to
  282. * reveal the now restored LB_BASE1/LB_MAP1 window.
  283. *
  284. * NOTE: We do not set up I2O mapping. I suspect that this is only
  285. * for an intelligent (target) device. Using I2O disables most of
  286. * the mappings into PCI memory.
  287. */
  288. static void __iomem *v3_map_bus(struct pci_bus *bus,
  289. unsigned int devfn, int offset)
  290. {
  291. struct v3_pci *v3 = bus->sysdata;
  292. unsigned int address, mapaddress, busnr;
  293. busnr = bus->number;
  294. if (busnr == 0) {
  295. int slot = PCI_SLOT(devfn);
  296. /*
  297. * local bus segment so need a type 0 config cycle
  298. *
  299. * build the PCI configuration "address" with one-hot in
  300. * A31-A11
  301. *
  302. * mapaddress:
  303. * 3:1 = config cycle (101)
  304. * 0 = PCI A1 & A0 are 0 (0)
  305. */
  306. address = PCI_FUNC(devfn) << 8;
  307. mapaddress = V3_LB_MAP_TYPE_CONFIG;
  308. if (slot > 12)
  309. /*
  310. * high order bits are handled by the MAP register
  311. */
  312. mapaddress |= BIT(slot - 5);
  313. else
  314. /*
  315. * low order bits handled directly in the address
  316. */
  317. address |= BIT(slot + 11);
  318. } else {
  319. /*
  320. * not the local bus segment so need a type 1 config cycle
  321. *
  322. * address:
  323. * 23:16 = bus number
  324. * 15:11 = slot number (7:3 of devfn)
  325. * 10:8 = func number (2:0 of devfn)
  326. *
  327. * mapaddress:
  328. * 3:1 = config cycle (101)
  329. * 0 = PCI A1 & A0 from host bus (1)
  330. */
  331. mapaddress = V3_LB_MAP_TYPE_CONFIG | V3_LB_MAP_AD_LOW_EN;
  332. address = (busnr << 16) | (devfn << 8);
  333. }
  334. /*
  335. * Set up base0 to see all 512Mbytes of memory space (not
  336. * prefetchable), this frees up base1 for re-use by
  337. * configuration memory
  338. */
  339. writel(v3_addr_to_lb_base(v3->non_pre_mem) |
  340. V3_LB_BASE_ADR_SIZE_512MB | V3_LB_BASE_ENABLE,
  341. v3->base + V3_LB_BASE0);
  342. /*
  343. * Set up base1/map1 to point into configuration space.
  344. * The config mem is always 16MB.
  345. */
  346. writel(v3_addr_to_lb_base(v3->config_mem) |
  347. V3_LB_BASE_ADR_SIZE_16MB | V3_LB_BASE_ENABLE,
  348. v3->base + V3_LB_BASE1);
  349. writew(mapaddress, v3->base + V3_LB_MAP1);
  350. return v3->config_base + address + offset;
  351. }
  352. static void v3_unmap_bus(struct v3_pci *v3)
  353. {
  354. /*
  355. * Reassign base1 for use by prefetchable PCI memory
  356. */
  357. writel(v3_addr_to_lb_base(v3->pre_mem) |
  358. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
  359. V3_LB_BASE_ENABLE,
  360. v3->base + V3_LB_BASE1);
  361. writew(v3_addr_to_lb_map(v3->pre_bus_addr) |
  362. V3_LB_MAP_TYPE_MEM, /* was V3_LB_MAP_TYPE_MEM_MULTIPLE */
  363. v3->base + V3_LB_MAP1);
  364. /*
  365. * And shrink base0 back to a 256M window (NOTE: MAP0 already correct)
  366. */
  367. writel(v3_addr_to_lb_base(v3->non_pre_mem) |
  368. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE,
  369. v3->base + V3_LB_BASE0);
  370. }
  371. static int v3_pci_read_config(struct pci_bus *bus, unsigned int fn,
  372. int config, int size, u32 *value)
  373. {
  374. struct v3_pci *v3 = bus->sysdata;
  375. int ret;
  376. dev_dbg(&bus->dev,
  377. "[read] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n",
  378. PCI_SLOT(fn), PCI_FUNC(fn), config, size, *value);
  379. ret = pci_generic_config_read(bus, fn, config, size, value);
  380. v3_unmap_bus(v3);
  381. return ret;
  382. }
  383. static int v3_pci_write_config(struct pci_bus *bus, unsigned int fn,
  384. int config, int size, u32 value)
  385. {
  386. struct v3_pci *v3 = bus->sysdata;
  387. int ret;
  388. dev_dbg(&bus->dev,
  389. "[write] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n",
  390. PCI_SLOT(fn), PCI_FUNC(fn), config, size, value);
  391. ret = pci_generic_config_write(bus, fn, config, size, value);
  392. v3_unmap_bus(v3);
  393. return ret;
  394. }
  395. static struct pci_ops v3_pci_ops = {
  396. .map_bus = v3_map_bus,
  397. .read = v3_pci_read_config,
  398. .write = v3_pci_write_config,
  399. };
  400. static irqreturn_t v3_irq(int irq, void *data)
  401. {
  402. struct v3_pci *v3 = data;
  403. struct device *dev = v3->dev;
  404. u32 status;
  405. status = readw(v3->base + V3_PCI_STAT);
  406. if (status & V3_PCI_STAT_PAR_ERR)
  407. dev_err(dev, "parity error interrupt\n");
  408. if (status & V3_PCI_STAT_SYS_ERR)
  409. dev_err(dev, "system error interrupt\n");
  410. if (status & V3_PCI_STAT_M_ABORT_ERR)
  411. dev_err(dev, "master abort error interrupt\n");
  412. if (status & V3_PCI_STAT_T_ABORT_ERR)
  413. dev_err(dev, "target abort error interrupt\n");
  414. writew(status, v3->base + V3_PCI_STAT);
  415. status = readb(v3->base + V3_LB_ISTAT);
  416. if (status & V3_LB_ISTAT_MAILBOX)
  417. dev_info(dev, "PCI mailbox interrupt\n");
  418. if (status & V3_LB_ISTAT_PCI_RD)
  419. dev_err(dev, "PCI target LB->PCI READ abort interrupt\n");
  420. if (status & V3_LB_ISTAT_PCI_WR)
  421. dev_err(dev, "PCI target LB->PCI WRITE abort interrupt\n");
  422. if (status & V3_LB_ISTAT_PCI_INT)
  423. dev_info(dev, "PCI pin interrupt\n");
  424. if (status & V3_LB_ISTAT_PCI_PERR)
  425. dev_err(dev, "PCI parity error interrupt\n");
  426. if (status & V3_LB_ISTAT_I2O_QWR)
  427. dev_info(dev, "I2O inbound post queue interrupt\n");
  428. if (status & V3_LB_ISTAT_DMA1)
  429. dev_info(dev, "DMA channel 1 interrupt\n");
  430. if (status & V3_LB_ISTAT_DMA0)
  431. dev_info(dev, "DMA channel 0 interrupt\n");
  432. /* Clear all possible interrupts on the local bus */
  433. writeb(0, v3->base + V3_LB_ISTAT);
  434. if (v3->map)
  435. regmap_write(v3->map, INTEGRATOR_SC_PCI_OFFSET,
  436. INTEGRATOR_SC_PCI_ENABLE |
  437. INTEGRATOR_SC_PCI_INTCLR);
  438. return IRQ_HANDLED;
  439. }
  440. static int v3_integrator_init(struct v3_pci *v3)
  441. {
  442. unsigned int val;
  443. v3->map =
  444. syscon_regmap_lookup_by_compatible("arm,integrator-ap-syscon");
  445. if (IS_ERR(v3->map)) {
  446. dev_err(v3->dev, "no syscon\n");
  447. return -ENODEV;
  448. }
  449. regmap_read(v3->map, INTEGRATOR_SC_PCI_OFFSET, &val);
  450. /* Take the PCI bridge out of reset, clear IRQs */
  451. regmap_write(v3->map, INTEGRATOR_SC_PCI_OFFSET,
  452. INTEGRATOR_SC_PCI_ENABLE |
  453. INTEGRATOR_SC_PCI_INTCLR);
  454. if (!(val & INTEGRATOR_SC_PCI_ENABLE)) {
  455. /* If we were in reset we need to sleep a bit */
  456. msleep(230);
  457. /* Set the physical base for the controller itself */
  458. writel(0x6200, v3->base + V3_LB_IO_BASE);
  459. /* Wait for the mailbox to settle after reset */
  460. do {
  461. writeb(0xaa, v3->base + V3_MAIL_DATA);
  462. writeb(0x55, v3->base + V3_MAIL_DATA + 4);
  463. } while (readb(v3->base + V3_MAIL_DATA) != 0xaa &&
  464. readb(v3->base + V3_MAIL_DATA) != 0x55);
  465. }
  466. dev_info(v3->dev, "initialized PCI V3 Integrator/AP integration\n");
  467. return 0;
  468. }
  469. static int v3_pci_setup_resource(struct v3_pci *v3,
  470. struct pci_host_bridge *host,
  471. struct resource_entry *win)
  472. {
  473. struct device *dev = v3->dev;
  474. struct resource *mem;
  475. struct resource *io;
  476. switch (resource_type(win->res)) {
  477. case IORESOURCE_IO:
  478. io = win->res;
  479. /* Setup window 2 - PCI I/O */
  480. writel(v3_addr_to_lb_base2(pci_pio_to_address(io->start)) |
  481. V3_LB_BASE2_ENABLE,
  482. v3->base + V3_LB_BASE2);
  483. writew(v3_addr_to_lb_map2(io->start - win->offset),
  484. v3->base + V3_LB_MAP2);
  485. break;
  486. case IORESOURCE_MEM:
  487. mem = win->res;
  488. if (mem->flags & IORESOURCE_PREFETCH) {
  489. mem->name = "V3 PCI PRE-MEM";
  490. v3->pre_mem = mem->start;
  491. v3->pre_bus_addr = mem->start - win->offset;
  492. dev_dbg(dev, "PREFETCHABLE MEM window %pR, bus addr %pap\n",
  493. mem, &v3->pre_bus_addr);
  494. if (resource_size(mem) != SZ_256M) {
  495. dev_err(dev, "prefetchable memory range is not 256MB\n");
  496. return -EINVAL;
  497. }
  498. if (v3->non_pre_mem &&
  499. (mem->start != v3->non_pre_mem + SZ_256M)) {
  500. dev_err(dev,
  501. "prefetchable memory is not adjacent to non-prefetchable memory\n");
  502. return -EINVAL;
  503. }
  504. /* Setup window 1 - PCI prefetchable memory */
  505. writel(v3_addr_to_lb_base(v3->pre_mem) |
  506. V3_LB_BASE_ADR_SIZE_256MB |
  507. V3_LB_BASE_PREFETCH |
  508. V3_LB_BASE_ENABLE,
  509. v3->base + V3_LB_BASE1);
  510. writew(v3_addr_to_lb_map(v3->pre_bus_addr) |
  511. V3_LB_MAP_TYPE_MEM, /* Was V3_LB_MAP_TYPE_MEM_MULTIPLE */
  512. v3->base + V3_LB_MAP1);
  513. } else {
  514. mem->name = "V3 PCI NON-PRE-MEM";
  515. v3->non_pre_mem = mem->start;
  516. v3->non_pre_bus_addr = mem->start - win->offset;
  517. dev_dbg(dev, "NON-PREFETCHABLE MEM window %pR, bus addr %pap\n",
  518. mem, &v3->non_pre_bus_addr);
  519. if (resource_size(mem) != SZ_256M) {
  520. dev_err(dev,
  521. "non-prefetchable memory range is not 256MB\n");
  522. return -EINVAL;
  523. }
  524. /* Setup window 0 - PCI non-prefetchable memory */
  525. writel(v3_addr_to_lb_base(v3->non_pre_mem) |
  526. V3_LB_BASE_ADR_SIZE_256MB |
  527. V3_LB_BASE_ENABLE,
  528. v3->base + V3_LB_BASE0);
  529. writew(v3_addr_to_lb_map(v3->non_pre_bus_addr) |
  530. V3_LB_MAP_TYPE_MEM,
  531. v3->base + V3_LB_MAP0);
  532. }
  533. break;
  534. case IORESOURCE_BUS:
  535. break;
  536. default:
  537. dev_info(dev, "Unknown resource type %lu\n",
  538. resource_type(win->res));
  539. break;
  540. }
  541. return 0;
  542. }
  543. static int v3_get_dma_range_config(struct v3_pci *v3,
  544. struct resource_entry *entry,
  545. u32 *pci_base, u32 *pci_map)
  546. {
  547. struct device *dev = v3->dev;
  548. u64 cpu_addr = entry->res->start;
  549. u64 cpu_end = entry->res->end;
  550. u64 pci_end = cpu_end - entry->offset;
  551. u64 pci_addr = entry->res->start - entry->offset;
  552. u32 val;
  553. if (pci_addr & ~V3_PCI_BASE_M_ADR_BASE) {
  554. dev_err(dev, "illegal range, only PCI bits 31..20 allowed\n");
  555. return -EINVAL;
  556. }
  557. val = ((u32)pci_addr) & V3_PCI_BASE_M_ADR_BASE;
  558. *pci_base = val;
  559. if (cpu_addr & ~V3_PCI_MAP_M_MAP_ADR) {
  560. dev_err(dev, "illegal range, only CPU bits 31..20 allowed\n");
  561. return -EINVAL;
  562. }
  563. val = ((u32)cpu_addr) & V3_PCI_MAP_M_MAP_ADR;
  564. switch (resource_size(entry->res)) {
  565. case SZ_1M:
  566. val |= V3_LB_BASE_ADR_SIZE_1MB;
  567. break;
  568. case SZ_2M:
  569. val |= V3_LB_BASE_ADR_SIZE_2MB;
  570. break;
  571. case SZ_4M:
  572. val |= V3_LB_BASE_ADR_SIZE_4MB;
  573. break;
  574. case SZ_8M:
  575. val |= V3_LB_BASE_ADR_SIZE_8MB;
  576. break;
  577. case SZ_16M:
  578. val |= V3_LB_BASE_ADR_SIZE_16MB;
  579. break;
  580. case SZ_32M:
  581. val |= V3_LB_BASE_ADR_SIZE_32MB;
  582. break;
  583. case SZ_64M:
  584. val |= V3_LB_BASE_ADR_SIZE_64MB;
  585. break;
  586. case SZ_128M:
  587. val |= V3_LB_BASE_ADR_SIZE_128MB;
  588. break;
  589. case SZ_256M:
  590. val |= V3_LB_BASE_ADR_SIZE_256MB;
  591. break;
  592. case SZ_512M:
  593. val |= V3_LB_BASE_ADR_SIZE_512MB;
  594. break;
  595. case SZ_1G:
  596. val |= V3_LB_BASE_ADR_SIZE_1GB;
  597. break;
  598. case SZ_2G:
  599. val |= V3_LB_BASE_ADR_SIZE_2GB;
  600. break;
  601. default:
  602. dev_err(v3->dev, "illegal dma memory chunk size\n");
  603. return -EINVAL;
  604. }
  605. val |= V3_PCI_MAP_M_REG_EN | V3_PCI_MAP_M_ENABLE;
  606. *pci_map = val;
  607. dev_dbg(dev,
  608. "DMA MEM CPU: 0x%016llx -> 0x%016llx => "
  609. "PCI: 0x%016llx -> 0x%016llx base %08x map %08x\n",
  610. cpu_addr, cpu_end,
  611. pci_addr, pci_end,
  612. *pci_base, *pci_map);
  613. return 0;
  614. }
  615. static int v3_pci_parse_map_dma_ranges(struct v3_pci *v3,
  616. struct device_node *np)
  617. {
  618. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(v3);
  619. struct device *dev = v3->dev;
  620. struct resource_entry *entry;
  621. int i = 0;
  622. resource_list_for_each_entry(entry, &bridge->dma_ranges) {
  623. int ret;
  624. u32 pci_base, pci_map;
  625. ret = v3_get_dma_range_config(v3, entry, &pci_base, &pci_map);
  626. if (ret)
  627. return ret;
  628. if (i == 0) {
  629. writel(pci_base, v3->base + V3_PCI_BASE0);
  630. writel(pci_map, v3->base + V3_PCI_MAP0);
  631. } else if (i == 1) {
  632. writel(pci_base, v3->base + V3_PCI_BASE1);
  633. writel(pci_map, v3->base + V3_PCI_MAP1);
  634. } else {
  635. dev_err(dev, "too many ranges, only two supported\n");
  636. dev_err(dev, "range %d ignored\n", i);
  637. }
  638. i++;
  639. }
  640. return 0;
  641. }
  642. static int v3_pci_probe(struct platform_device *pdev)
  643. {
  644. struct device *dev = &pdev->dev;
  645. struct device_node *np = dev->of_node;
  646. struct resource *regs;
  647. struct resource_entry *win;
  648. struct v3_pci *v3;
  649. struct pci_host_bridge *host;
  650. struct clk *clk;
  651. u16 val;
  652. int irq;
  653. int ret;
  654. host = devm_pci_alloc_host_bridge(dev, sizeof(*v3));
  655. if (!host)
  656. return -ENOMEM;
  657. host->ops = &v3_pci_ops;
  658. v3 = pci_host_bridge_priv(host);
  659. host->sysdata = v3;
  660. v3->dev = dev;
  661. /* Get and enable host clock */
  662. clk = devm_clk_get(dev, NULL);
  663. if (IS_ERR(clk)) {
  664. dev_err(dev, "clock not found\n");
  665. return PTR_ERR(clk);
  666. }
  667. ret = clk_prepare_enable(clk);
  668. if (ret) {
  669. dev_err(dev, "unable to enable clock\n");
  670. return ret;
  671. }
  672. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  673. v3->base = devm_ioremap_resource(dev, regs);
  674. if (IS_ERR(v3->base))
  675. return PTR_ERR(v3->base);
  676. /*
  677. * The hardware has a register with the physical base address
  678. * of the V3 controller itself, verify that this is the same
  679. * as the physical memory we've remapped it from.
  680. */
  681. if (readl(v3->base + V3_LB_IO_BASE) != (regs->start >> 16))
  682. dev_err(dev, "V3_LB_IO_BASE = %08x but device is @%pR\n",
  683. readl(v3->base + V3_LB_IO_BASE), regs);
  684. /* Configuration space is 16MB directly mapped */
  685. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  686. if (resource_size(regs) != SZ_16M) {
  687. dev_err(dev, "config mem is not 16MB!\n");
  688. return -EINVAL;
  689. }
  690. v3->config_mem = regs->start;
  691. v3->config_base = devm_ioremap_resource(dev, regs);
  692. if (IS_ERR(v3->config_base))
  693. return PTR_ERR(v3->config_base);
  694. /* Get and request error IRQ resource */
  695. irq = platform_get_irq(pdev, 0);
  696. if (irq < 0)
  697. return irq;
  698. ret = devm_request_irq(dev, irq, v3_irq, 0,
  699. "PCIv3 error", v3);
  700. if (ret < 0) {
  701. dev_err(dev,
  702. "unable to request PCIv3 error IRQ %d (%d)\n",
  703. irq, ret);
  704. return ret;
  705. }
  706. /*
  707. * Unlock V3 registers, but only if they were previously locked.
  708. */
  709. if (readw(v3->base + V3_SYSTEM) & V3_SYSTEM_M_LOCK)
  710. writew(V3_SYSTEM_UNLOCK, v3->base + V3_SYSTEM);
  711. /* Disable all slave access while we set up the windows */
  712. val = readw(v3->base + V3_PCI_CMD);
  713. val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  714. writew(val, v3->base + V3_PCI_CMD);
  715. /* Put the PCI bus into reset */
  716. val = readw(v3->base + V3_SYSTEM);
  717. val &= ~V3_SYSTEM_M_RST_OUT;
  718. writew(val, v3->base + V3_SYSTEM);
  719. /* Retry until we're ready */
  720. val = readw(v3->base + V3_PCI_CFG);
  721. val |= V3_PCI_CFG_M_RETRY_EN;
  722. writew(val, v3->base + V3_PCI_CFG);
  723. /* Set up the local bus protocol */
  724. val = readw(v3->base + V3_LB_CFG);
  725. val |= V3_LB_CFG_LB_BE_IMODE; /* Byte enable input */
  726. val |= V3_LB_CFG_LB_BE_OMODE; /* Byte enable output */
  727. val &= ~V3_LB_CFG_LB_ENDIAN; /* Little endian */
  728. val &= ~V3_LB_CFG_LB_PPC_RDY; /* TODO: when using on PPC403Gx, set to 1 */
  729. writew(val, v3->base + V3_LB_CFG);
  730. /* Enable the PCI bus master */
  731. val = readw(v3->base + V3_PCI_CMD);
  732. val |= PCI_COMMAND_MASTER;
  733. writew(val, v3->base + V3_PCI_CMD);
  734. /* Get the I/O and memory ranges from DT */
  735. resource_list_for_each_entry(win, &host->windows) {
  736. ret = v3_pci_setup_resource(v3, host, win);
  737. if (ret) {
  738. dev_err(dev, "error setting up resources\n");
  739. return ret;
  740. }
  741. }
  742. ret = v3_pci_parse_map_dma_ranges(v3, np);
  743. if (ret)
  744. return ret;
  745. /*
  746. * Disable PCI to host IO cycles, enable I/O buffers @3.3V,
  747. * set AD_LOW0 to 1 if one of the LB_MAP registers choose
  748. * to use this (should be unused).
  749. */
  750. writel(0x00000000, v3->base + V3_PCI_IO_BASE);
  751. val = V3_PCI_CFG_M_IO_REG_DIS | V3_PCI_CFG_M_IO_DIS |
  752. V3_PCI_CFG_M_EN3V | V3_PCI_CFG_M_AD_LOW0;
  753. /*
  754. * DMA read and write from PCI bus commands types
  755. */
  756. val |= V3_PCI_CFG_TYPE_DEFAULT << V3_PCI_CFG_M_RTYPE_SHIFT;
  757. val |= V3_PCI_CFG_TYPE_DEFAULT << V3_PCI_CFG_M_WTYPE_SHIFT;
  758. writew(val, v3->base + V3_PCI_CFG);
  759. /*
  760. * Set the V3 FIFO such that writes have higher priority than
  761. * reads, and local bus write causes local bus read fifo flush
  762. * on aperture 1. Same for PCI.
  763. */
  764. writew(V3_FIFO_PRIO_LB_RD1_FLUSH_AP1 |
  765. V3_FIFO_PRIO_LB_RD0_FLUSH_AP1 |
  766. V3_FIFO_PRIO_PCI_RD1_FLUSH_AP1 |
  767. V3_FIFO_PRIO_PCI_RD0_FLUSH_AP1,
  768. v3->base + V3_FIFO_PRIORITY);
  769. /*
  770. * Clear any error interrupts, and enable parity and write error
  771. * interrupts
  772. */
  773. writeb(0, v3->base + V3_LB_ISTAT);
  774. val = readw(v3->base + V3_LB_CFG);
  775. val |= V3_LB_CFG_LB_LB_INT;
  776. writew(val, v3->base + V3_LB_CFG);
  777. writeb(V3_LB_ISTAT_PCI_WR | V3_LB_ISTAT_PCI_PERR,
  778. v3->base + V3_LB_IMASK);
  779. /* Special Integrator initialization */
  780. if (of_device_is_compatible(np, "arm,integrator-ap-pci")) {
  781. ret = v3_integrator_init(v3);
  782. if (ret)
  783. return ret;
  784. }
  785. /* Post-init: enable PCI memory and invalidate (master already on) */
  786. val = readw(v3->base + V3_PCI_CMD);
  787. val |= PCI_COMMAND_MEMORY | PCI_COMMAND_INVALIDATE;
  788. writew(val, v3->base + V3_PCI_CMD);
  789. /* Clear pending interrupts */
  790. writeb(0, v3->base + V3_LB_ISTAT);
  791. /* Read or write errors and parity errors cause interrupts */
  792. writeb(V3_LB_ISTAT_PCI_RD | V3_LB_ISTAT_PCI_WR | V3_LB_ISTAT_PCI_PERR,
  793. v3->base + V3_LB_IMASK);
  794. /* Take the PCI bus out of reset so devices can initialize */
  795. val = readw(v3->base + V3_SYSTEM);
  796. val |= V3_SYSTEM_M_RST_OUT;
  797. writew(val, v3->base + V3_SYSTEM);
  798. /*
  799. * Re-lock the system register.
  800. */
  801. val = readw(v3->base + V3_SYSTEM);
  802. val |= V3_SYSTEM_M_LOCK;
  803. writew(val, v3->base + V3_SYSTEM);
  804. return pci_host_probe(host);
  805. }
  806. static const struct of_device_id v3_pci_of_match[] = {
  807. {
  808. .compatible = "v3,v360epc-pci",
  809. },
  810. {},
  811. };
  812. static struct platform_driver v3_pci_driver = {
  813. .driver = {
  814. .name = "pci-v3-semi",
  815. .of_match_table = of_match_ptr(v3_pci_of_match),
  816. .suppress_bind_attrs = true,
  817. },
  818. .probe = v3_pci_probe,
  819. };
  820. builtin_platform_driver(v3_pci_driver);