pci_v3.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * linux/arch/arm/mach-integrator/pci_v3.c
  3. *
  4. * PCI functions for V3 host PCI bridge
  5. *
  6. * Copyright (C) 1999 ARM Limited
  7. * Copyright (C) 2000-2001 Deep Blue Solutions Ltd
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/ioport.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/init.h>
  29. #include <linux/io.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/of.h>
  32. #include <linux/of_address.h>
  33. #include <linux/of_irq.h>
  34. #include <linux/of_pci.h>
  35. #include <video/vga.h>
  36. #include <mach/hardware.h>
  37. #include <mach/platform.h>
  38. #include <mach/irqs.h>
  39. #include <asm/mach/map.h>
  40. #include <asm/signal.h>
  41. #include <asm/mach/pci.h>
  42. #include <asm/irq_regs.h>
  43. #include "pci_v3.h"
  44. /*
  45. * Where in the memory map does PCI live?
  46. *
  47. * This represents a fairly liberal usage of address space. Even though
  48. * the V3 only has two windows (therefore we need to map stuff on the fly),
  49. * we maintain the same addresses, even if they're not mapped.
  50. */
  51. #define PHYS_PCI_MEM_BASE 0x40000000 /* 256M */
  52. #define PHYS_PCI_PRE_BASE 0x50000000 /* 256M */
  53. #define PHYS_PCI_IO_BASE 0x60000000 /* 16M */
  54. #define PHYS_PCI_CONFIG_BASE 0x61000000 /* 16M */
  55. #define PHYS_PCI_V3_BASE 0x62000000 /* 64K */
  56. #define PCI_MEMORY_VADDR IOMEM(0xe8000000)
  57. #define PCI_CONFIG_VADDR IOMEM(0xec000000)
  58. /*
  59. * V3 Local Bus to PCI Bridge definitions
  60. *
  61. * Registers (these are taken from page 129 of the EPC User's Manual Rev 1.04
  62. * All V3 register names are prefaced by V3_ to avoid clashing with any other
  63. * PCI definitions. Their names match the user's manual.
  64. *
  65. * I'm assuming that I20 is disabled.
  66. *
  67. */
  68. #define V3_PCI_VENDOR 0x00000000
  69. #define V3_PCI_DEVICE 0x00000002
  70. #define V3_PCI_CMD 0x00000004
  71. #define V3_PCI_STAT 0x00000006
  72. #define V3_PCI_CC_REV 0x00000008
  73. #define V3_PCI_HDR_CFG 0x0000000C
  74. #define V3_PCI_IO_BASE 0x00000010
  75. #define V3_PCI_BASE0 0x00000014
  76. #define V3_PCI_BASE1 0x00000018
  77. #define V3_PCI_SUB_VENDOR 0x0000002C
  78. #define V3_PCI_SUB_ID 0x0000002E
  79. #define V3_PCI_ROM 0x00000030
  80. #define V3_PCI_BPARAM 0x0000003C
  81. #define V3_PCI_MAP0 0x00000040
  82. #define V3_PCI_MAP1 0x00000044
  83. #define V3_PCI_INT_STAT 0x00000048
  84. #define V3_PCI_INT_CFG 0x0000004C
  85. #define V3_LB_BASE0 0x00000054
  86. #define V3_LB_BASE1 0x00000058
  87. #define V3_LB_MAP0 0x0000005E
  88. #define V3_LB_MAP1 0x00000062
  89. #define V3_LB_BASE2 0x00000064
  90. #define V3_LB_MAP2 0x00000066
  91. #define V3_LB_SIZE 0x00000068
  92. #define V3_LB_IO_BASE 0x0000006E
  93. #define V3_FIFO_CFG 0x00000070
  94. #define V3_FIFO_PRIORITY 0x00000072
  95. #define V3_FIFO_STAT 0x00000074
  96. #define V3_LB_ISTAT 0x00000076
  97. #define V3_LB_IMASK 0x00000077
  98. #define V3_SYSTEM 0x00000078
  99. #define V3_LB_CFG 0x0000007A
  100. #define V3_PCI_CFG 0x0000007C
  101. #define V3_DMA_PCI_ADR0 0x00000080
  102. #define V3_DMA_PCI_ADR1 0x00000090
  103. #define V3_DMA_LOCAL_ADR0 0x00000084
  104. #define V3_DMA_LOCAL_ADR1 0x00000094
  105. #define V3_DMA_LENGTH0 0x00000088
  106. #define V3_DMA_LENGTH1 0x00000098
  107. #define V3_DMA_CSR0 0x0000008B
  108. #define V3_DMA_CSR1 0x0000009B
  109. #define V3_DMA_CTLB_ADR0 0x0000008C
  110. #define V3_DMA_CTLB_ADR1 0x0000009C
  111. #define V3_DMA_DELAY 0x000000E0
  112. #define V3_MAIL_DATA 0x000000C0
  113. #define V3_PCI_MAIL_IEWR 0x000000D0
  114. #define V3_PCI_MAIL_IERD 0x000000D2
  115. #define V3_LB_MAIL_IEWR 0x000000D4
  116. #define V3_LB_MAIL_IERD 0x000000D6
  117. #define V3_MAIL_WR_STAT 0x000000D8
  118. #define V3_MAIL_RD_STAT 0x000000DA
  119. #define V3_QBA_MAP 0x000000DC
  120. /* PCI COMMAND REGISTER bits
  121. */
  122. #define V3_COMMAND_M_FBB_EN (1 << 9)
  123. #define V3_COMMAND_M_SERR_EN (1 << 8)
  124. #define V3_COMMAND_M_PAR_EN (1 << 6)
  125. #define V3_COMMAND_M_MASTER_EN (1 << 2)
  126. #define V3_COMMAND_M_MEM_EN (1 << 1)
  127. #define V3_COMMAND_M_IO_EN (1 << 0)
  128. /* SYSTEM REGISTER bits
  129. */
  130. #define V3_SYSTEM_M_RST_OUT (1 << 15)
  131. #define V3_SYSTEM_M_LOCK (1 << 14)
  132. /* PCI_CFG bits
  133. */
  134. #define V3_PCI_CFG_M_I2O_EN (1 << 15)
  135. #define V3_PCI_CFG_M_IO_REG_DIS (1 << 14)
  136. #define V3_PCI_CFG_M_IO_DIS (1 << 13)
  137. #define V3_PCI_CFG_M_EN3V (1 << 12)
  138. #define V3_PCI_CFG_M_RETRY_EN (1 << 10)
  139. #define V3_PCI_CFG_M_AD_LOW1 (1 << 9)
  140. #define V3_PCI_CFG_M_AD_LOW0 (1 << 8)
  141. /* PCI_BASE register bits (PCI -> Local Bus)
  142. */
  143. #define V3_PCI_BASE_M_ADR_BASE 0xFFF00000
  144. #define V3_PCI_BASE_M_ADR_BASEL 0x000FFF00
  145. #define V3_PCI_BASE_M_PREFETCH (1 << 3)
  146. #define V3_PCI_BASE_M_TYPE (3 << 1)
  147. #define V3_PCI_BASE_M_IO (1 << 0)
  148. /* PCI MAP register bits (PCI -> Local bus)
  149. */
  150. #define V3_PCI_MAP_M_MAP_ADR 0xFFF00000
  151. #define V3_PCI_MAP_M_RD_POST_INH (1 << 15)
  152. #define V3_PCI_MAP_M_ROM_SIZE (3 << 10)
  153. #define V3_PCI_MAP_M_SWAP (3 << 8)
  154. #define V3_PCI_MAP_M_ADR_SIZE 0x000000F0
  155. #define V3_PCI_MAP_M_REG_EN (1 << 1)
  156. #define V3_PCI_MAP_M_ENABLE (1 << 0)
  157. /*
  158. * LB_BASE0,1 register bits (Local bus -> PCI)
  159. */
  160. #define V3_LB_BASE_ADR_BASE 0xfff00000
  161. #define V3_LB_BASE_SWAP (3 << 8)
  162. #define V3_LB_BASE_ADR_SIZE (15 << 4)
  163. #define V3_LB_BASE_PREFETCH (1 << 3)
  164. #define V3_LB_BASE_ENABLE (1 << 0)
  165. #define V3_LB_BASE_ADR_SIZE_1MB (0 << 4)
  166. #define V3_LB_BASE_ADR_SIZE_2MB (1 << 4)
  167. #define V3_LB_BASE_ADR_SIZE_4MB (2 << 4)
  168. #define V3_LB_BASE_ADR_SIZE_8MB (3 << 4)
  169. #define V3_LB_BASE_ADR_SIZE_16MB (4 << 4)
  170. #define V3_LB_BASE_ADR_SIZE_32MB (5 << 4)
  171. #define V3_LB_BASE_ADR_SIZE_64MB (6 << 4)
  172. #define V3_LB_BASE_ADR_SIZE_128MB (7 << 4)
  173. #define V3_LB_BASE_ADR_SIZE_256MB (8 << 4)
  174. #define V3_LB_BASE_ADR_SIZE_512MB (9 << 4)
  175. #define V3_LB_BASE_ADR_SIZE_1GB (10 << 4)
  176. #define V3_LB_BASE_ADR_SIZE_2GB (11 << 4)
  177. #define v3_addr_to_lb_base(a) ((a) & V3_LB_BASE_ADR_BASE)
  178. /*
  179. * LB_MAP0,1 register bits (Local bus -> PCI)
  180. */
  181. #define V3_LB_MAP_MAP_ADR 0xfff0
  182. #define V3_LB_MAP_TYPE (7 << 1)
  183. #define V3_LB_MAP_AD_LOW_EN (1 << 0)
  184. #define V3_LB_MAP_TYPE_IACK (0 << 1)
  185. #define V3_LB_MAP_TYPE_IO (1 << 1)
  186. #define V3_LB_MAP_TYPE_MEM (3 << 1)
  187. #define V3_LB_MAP_TYPE_CONFIG (5 << 1)
  188. #define V3_LB_MAP_TYPE_MEM_MULTIPLE (6 << 1)
  189. #define v3_addr_to_lb_map(a) (((a) >> 16) & V3_LB_MAP_MAP_ADR)
  190. /*
  191. * LB_BASE2 register bits (Local bus -> PCI IO)
  192. */
  193. #define V3_LB_BASE2_ADR_BASE 0xff00
  194. #define V3_LB_BASE2_SWAP (3 << 6)
  195. #define V3_LB_BASE2_ENABLE (1 << 0)
  196. #define v3_addr_to_lb_base2(a) (((a) >> 16) & V3_LB_BASE2_ADR_BASE)
  197. /*
  198. * LB_MAP2 register bits (Local bus -> PCI IO)
  199. */
  200. #define V3_LB_MAP2_MAP_ADR 0xff00
  201. #define v3_addr_to_lb_map2(a) (((a) >> 16) & V3_LB_MAP2_MAP_ADR)
  202. /*
  203. * The V3 PCI interface chip in Integrator provides several windows from
  204. * local bus memory into the PCI memory areas. Unfortunately, there
  205. * are not really enough windows for our usage, therefore we reuse
  206. * one of the windows for access to PCI configuration space. The
  207. * memory map is as follows:
  208. *
  209. * Local Bus Memory Usage
  210. *
  211. * 40000000 - 4FFFFFFF PCI memory. 256M non-prefetchable
  212. * 50000000 - 5FFFFFFF PCI memory. 256M prefetchable
  213. * 60000000 - 60FFFFFF PCI IO. 16M
  214. * 61000000 - 61FFFFFF PCI Configuration. 16M
  215. *
  216. * There are three V3 windows, each described by a pair of V3 registers.
  217. * These are LB_BASE0/LB_MAP0, LB_BASE1/LB_MAP1 and LB_BASE2/LB_MAP2.
  218. * Base0 and Base1 can be used for any type of PCI memory access. Base2
  219. * can be used either for PCI I/O or for I20 accesses. By default, uHAL
  220. * uses this only for PCI IO space.
  221. *
  222. * Normally these spaces are mapped using the following base registers:
  223. *
  224. * Usage Local Bus Memory Base/Map registers used
  225. *
  226. * Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP0
  227. * Mem 50000000 - 5FFFFFFF LB_BASE1/LB_MAP1
  228. * IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP2
  229. * Cfg 61000000 - 61FFFFFF
  230. *
  231. * This means that I20 and PCI configuration space accesses will fail.
  232. * When PCI configuration accesses are needed (via the uHAL PCI
  233. * configuration space primitives) we must remap the spaces as follows:
  234. *
  235. * Usage Local Bus Memory Base/Map registers used
  236. *
  237. * Mem 40000000 - 4FFFFFFF LB_BASE0/LB_MAP0
  238. * Mem 50000000 - 5FFFFFFF LB_BASE0/LB_MAP0
  239. * IO 60000000 - 60FFFFFF LB_BASE2/LB_MAP2
  240. * Cfg 61000000 - 61FFFFFF LB_BASE1/LB_MAP1
  241. *
  242. * To make this work, the code depends on overlapping windows working.
  243. * The V3 chip translates an address by checking its range within
  244. * each of the BASE/MAP pairs in turn (in ascending register number
  245. * order). It will use the first matching pair. So, for example,
  246. * if the same address is mapped by both LB_BASE0/LB_MAP0 and
  247. * LB_BASE1/LB_MAP1, the V3 will use the translation from
  248. * LB_BASE0/LB_MAP0.
  249. *
  250. * To allow PCI Configuration space access, the code enlarges the
  251. * window mapped by LB_BASE0/LB_MAP0 from 256M to 512M. This occludes
  252. * the windows currently mapped by LB_BASE1/LB_MAP1 so that it can
  253. * be remapped for use by configuration cycles.
  254. *
  255. * At the end of the PCI Configuration space accesses,
  256. * LB_BASE1/LB_MAP1 is reset to map PCI Memory. Finally the window
  257. * mapped by LB_BASE0/LB_MAP0 is reduced in size from 512M to 256M to
  258. * reveal the now restored LB_BASE1/LB_MAP1 window.
  259. *
  260. * NOTE: We do not set up I2O mapping. I suspect that this is only
  261. * for an intelligent (target) device. Using I2O disables most of
  262. * the mappings into PCI memory.
  263. */
  264. /* Filled in by probe */
  265. static void __iomem *pci_v3_base;
  266. /* CPU side memory ranges */
  267. static struct resource conf_mem; /* FIXME: remap this instead of static map */
  268. static struct resource io_mem;
  269. static struct resource non_mem;
  270. static struct resource pre_mem;
  271. /* PCI side memory ranges */
  272. static u64 non_mem_pci;
  273. static u64 non_mem_pci_sz;
  274. static u64 pre_mem_pci;
  275. static u64 pre_mem_pci_sz;
  276. // V3 access routines
  277. #define v3_writeb(o,v) __raw_writeb(v, pci_v3_base + (unsigned int)(o))
  278. #define v3_readb(o) (__raw_readb(pci_v3_base + (unsigned int)(o)))
  279. #define v3_writew(o,v) __raw_writew(v, pci_v3_base + (unsigned int)(o))
  280. #define v3_readw(o) (__raw_readw(pci_v3_base + (unsigned int)(o)))
  281. #define v3_writel(o,v) __raw_writel(v, pci_v3_base + (unsigned int)(o))
  282. #define v3_readl(o) (__raw_readl(pci_v3_base + (unsigned int)(o)))
  283. /*============================================================================
  284. *
  285. * routine: uHALir_PCIMakeConfigAddress()
  286. *
  287. * parameters: bus = which bus
  288. * device = which device
  289. * function = which function
  290. * offset = configuration space register we are interested in
  291. *
  292. * description: this routine will generate a platform dependent config
  293. * address.
  294. *
  295. * calls: none
  296. *
  297. * returns: configuration address to play on the PCI bus
  298. *
  299. * To generate the appropriate PCI configuration cycles in the PCI
  300. * configuration address space, you present the V3 with the following pattern
  301. * (which is very nearly a type 1 (except that the lower two bits are 00 and
  302. * not 01). In order for this mapping to work you need to set up one of
  303. * the local to PCI aperatures to 16Mbytes in length translating to
  304. * PCI configuration space starting at 0x0000.0000.
  305. *
  306. * PCI configuration cycles look like this:
  307. *
  308. * Type 0:
  309. *
  310. * 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1
  311. * 3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  312. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  313. * | | |D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|0|
  314. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  315. *
  316. * 31:11 Device select bit.
  317. * 10:8 Function number
  318. * 7:2 Register number
  319. *
  320. * Type 1:
  321. *
  322. * 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1
  323. * 3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  324. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  325. * | | | | | | | | | | |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|1|
  326. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  327. *
  328. * 31:24 reserved
  329. * 23:16 bus number (8 bits = 128 possible buses)
  330. * 15:11 Device number (5 bits)
  331. * 10:8 function number
  332. * 7:2 register number
  333. *
  334. */
  335. static DEFINE_RAW_SPINLOCK(v3_lock);
  336. #undef V3_LB_BASE_PREFETCH
  337. #define V3_LB_BASE_PREFETCH 0
  338. static void __iomem *v3_open_config_window(struct pci_bus *bus,
  339. unsigned int devfn, int offset)
  340. {
  341. unsigned int address, mapaddress, busnr;
  342. busnr = bus->number;
  343. /*
  344. * Trap out illegal values
  345. */
  346. BUG_ON(offset > 255);
  347. BUG_ON(busnr > 255);
  348. BUG_ON(devfn > 255);
  349. if (busnr == 0) {
  350. int slot = PCI_SLOT(devfn);
  351. /*
  352. * local bus segment so need a type 0 config cycle
  353. *
  354. * build the PCI configuration "address" with one-hot in
  355. * A31-A11
  356. *
  357. * mapaddress:
  358. * 3:1 = config cycle (101)
  359. * 0 = PCI A1 & A0 are 0 (0)
  360. */
  361. address = PCI_FUNC(devfn) << 8;
  362. mapaddress = V3_LB_MAP_TYPE_CONFIG;
  363. if (slot > 12)
  364. /*
  365. * high order bits are handled by the MAP register
  366. */
  367. mapaddress |= 1 << (slot - 5);
  368. else
  369. /*
  370. * low order bits handled directly in the address
  371. */
  372. address |= 1 << (slot + 11);
  373. } else {
  374. /*
  375. * not the local bus segment so need a type 1 config cycle
  376. *
  377. * address:
  378. * 23:16 = bus number
  379. * 15:11 = slot number (7:3 of devfn)
  380. * 10:8 = func number (2:0 of devfn)
  381. *
  382. * mapaddress:
  383. * 3:1 = config cycle (101)
  384. * 0 = PCI A1 & A0 from host bus (1)
  385. */
  386. mapaddress = V3_LB_MAP_TYPE_CONFIG | V3_LB_MAP_AD_LOW_EN;
  387. address = (busnr << 16) | (devfn << 8);
  388. }
  389. /*
  390. * Set up base0 to see all 512Mbytes of memory space (not
  391. * prefetchable), this frees up base1 for re-use by
  392. * configuration memory
  393. */
  394. v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(non_mem.start) |
  395. V3_LB_BASE_ADR_SIZE_512MB | V3_LB_BASE_ENABLE);
  396. /*
  397. * Set up base1/map1 to point into configuration space.
  398. */
  399. v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(conf_mem.start) |
  400. V3_LB_BASE_ADR_SIZE_16MB | V3_LB_BASE_ENABLE);
  401. v3_writew(V3_LB_MAP1, mapaddress);
  402. return PCI_CONFIG_VADDR + address + offset;
  403. }
  404. static void v3_close_config_window(void)
  405. {
  406. /*
  407. * Reassign base1 for use by prefetchable PCI memory
  408. */
  409. v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(pre_mem.start) |
  410. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
  411. V3_LB_BASE_ENABLE);
  412. v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(pre_mem_pci) |
  413. V3_LB_MAP_TYPE_MEM_MULTIPLE);
  414. /*
  415. * And shrink base0 back to a 256M window (NOTE: MAP0 already correct)
  416. */
  417. v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(non_mem.start) |
  418. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
  419. }
  420. static int v3_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  421. int size, u32 *val)
  422. {
  423. void __iomem *addr;
  424. unsigned long flags;
  425. u32 v;
  426. raw_spin_lock_irqsave(&v3_lock, flags);
  427. addr = v3_open_config_window(bus, devfn, where);
  428. switch (size) {
  429. case 1:
  430. v = __raw_readb(addr);
  431. break;
  432. case 2:
  433. v = __raw_readw(addr);
  434. break;
  435. default:
  436. v = __raw_readl(addr);
  437. break;
  438. }
  439. v3_close_config_window();
  440. raw_spin_unlock_irqrestore(&v3_lock, flags);
  441. *val = v;
  442. return PCIBIOS_SUCCESSFUL;
  443. }
  444. static int v3_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  445. int size, u32 val)
  446. {
  447. void __iomem *addr;
  448. unsigned long flags;
  449. raw_spin_lock_irqsave(&v3_lock, flags);
  450. addr = v3_open_config_window(bus, devfn, where);
  451. switch (size) {
  452. case 1:
  453. __raw_writeb((u8)val, addr);
  454. __raw_readb(addr);
  455. break;
  456. case 2:
  457. __raw_writew((u16)val, addr);
  458. __raw_readw(addr);
  459. break;
  460. case 4:
  461. __raw_writel(val, addr);
  462. __raw_readl(addr);
  463. break;
  464. }
  465. v3_close_config_window();
  466. raw_spin_unlock_irqrestore(&v3_lock, flags);
  467. return PCIBIOS_SUCCESSFUL;
  468. }
  469. static struct pci_ops pci_v3_ops = {
  470. .read = v3_read_config,
  471. .write = v3_write_config,
  472. };
  473. static int __init pci_v3_setup_resources(struct pci_sys_data *sys)
  474. {
  475. if (request_resource(&iomem_resource, &non_mem)) {
  476. printk(KERN_ERR "PCI: unable to allocate non-prefetchable "
  477. "memory region\n");
  478. return -EBUSY;
  479. }
  480. if (request_resource(&iomem_resource, &pre_mem)) {
  481. release_resource(&non_mem);
  482. printk(KERN_ERR "PCI: unable to allocate prefetchable "
  483. "memory region\n");
  484. return -EBUSY;
  485. }
  486. /*
  487. * the mem resource for this bus
  488. * the prefetch mem resource for this bus
  489. */
  490. pci_add_resource_offset(&sys->resources, &non_mem, sys->mem_offset);
  491. pci_add_resource_offset(&sys->resources, &pre_mem, sys->mem_offset);
  492. return 1;
  493. }
  494. /*
  495. * These don't seem to be implemented on the Integrator I have, which
  496. * means I can't get additional information on the reason for the pm2fb
  497. * problems. I suppose I'll just have to mind-meld with the machine. ;)
  498. */
  499. static void __iomem *ap_syscon_base;
  500. #define INTEGRATOR_SC_PCIENABLE_OFFSET 0x18
  501. #define INTEGRATOR_SC_LBFADDR_OFFSET 0x20
  502. #define INTEGRATOR_SC_LBFCODE_OFFSET 0x24
  503. static int
  504. v3_pci_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  505. {
  506. unsigned long pc = instruction_pointer(regs);
  507. unsigned long instr = *(unsigned long *)pc;
  508. #if 0
  509. char buf[128];
  510. sprintf(buf, "V3 fault: addr 0x%08lx, FSR 0x%03x, PC 0x%08lx [%08lx] LBFADDR=%08x LBFCODE=%02x ISTAT=%02x\n",
  511. addr, fsr, pc, instr, __raw_readl(ap_syscon_base + INTEGRATOR_SC_LBFADDR_OFFSET), __raw_readl(ap_syscon_base + INTEGRATOR_SC_LBFCODE_OFFSET) & 255,
  512. v3_readb(V3_LB_ISTAT));
  513. printk(KERN_DEBUG "%s", buf);
  514. #endif
  515. v3_writeb(V3_LB_ISTAT, 0);
  516. __raw_writel(3, ap_syscon_base + INTEGRATOR_SC_PCIENABLE_OFFSET);
  517. /*
  518. * If the instruction being executed was a read,
  519. * make it look like it read all-ones.
  520. */
  521. if ((instr & 0x0c100000) == 0x04100000) {
  522. int reg = (instr >> 12) & 15;
  523. unsigned long val;
  524. if (instr & 0x00400000)
  525. val = 255;
  526. else
  527. val = -1;
  528. regs->uregs[reg] = val;
  529. regs->ARM_pc += 4;
  530. return 0;
  531. }
  532. if ((instr & 0x0e100090) == 0x00100090) {
  533. int reg = (instr >> 12) & 15;
  534. regs->uregs[reg] = -1;
  535. regs->ARM_pc += 4;
  536. return 0;
  537. }
  538. return 1;
  539. }
  540. static irqreturn_t v3_irq(int dummy, void *devid)
  541. {
  542. #ifdef CONFIG_DEBUG_LL
  543. struct pt_regs *regs = get_irq_regs();
  544. unsigned long pc = instruction_pointer(regs);
  545. unsigned long instr = *(unsigned long *)pc;
  546. char buf[128];
  547. extern void printascii(const char *);
  548. sprintf(buf, "V3 int %d: pc=0x%08lx [%08lx] LBFADDR=%08x LBFCODE=%02x "
  549. "ISTAT=%02x\n", IRQ_AP_V3INT, pc, instr,
  550. __raw_readl(ap_syscon_base + INTEGRATOR_SC_LBFADDR_OFFSET),
  551. __raw_readl(ap_syscon_base + INTEGRATOR_SC_LBFCODE_OFFSET) & 255,
  552. v3_readb(V3_LB_ISTAT));
  553. printascii(buf);
  554. #endif
  555. v3_writew(V3_PCI_STAT, 0xf000);
  556. v3_writeb(V3_LB_ISTAT, 0);
  557. __raw_writel(3, ap_syscon_base + INTEGRATOR_SC_PCIENABLE_OFFSET);
  558. #ifdef CONFIG_DEBUG_LL
  559. /*
  560. * If the instruction being executed was a read,
  561. * make it look like it read all-ones.
  562. */
  563. if ((instr & 0x0c100000) == 0x04100000) {
  564. int reg = (instr >> 16) & 15;
  565. sprintf(buf, " reg%d = %08lx\n", reg, regs->uregs[reg]);
  566. printascii(buf);
  567. }
  568. #endif
  569. return IRQ_HANDLED;
  570. }
  571. static int __init pci_v3_setup(int nr, struct pci_sys_data *sys)
  572. {
  573. int ret = 0;
  574. if (!ap_syscon_base)
  575. return -EINVAL;
  576. if (nr == 0) {
  577. sys->mem_offset = non_mem.start;
  578. ret = pci_v3_setup_resources(sys);
  579. }
  580. return ret;
  581. }
  582. /*
  583. * V3_LB_BASE? - local bus address
  584. * V3_LB_MAP? - pci bus address
  585. */
  586. static void __init pci_v3_preinit(void)
  587. {
  588. unsigned long flags;
  589. unsigned int temp;
  590. pcibios_min_mem = 0x00100000;
  591. /*
  592. * Hook in our fault handler for PCI errors
  593. */
  594. hook_fault_code(4, v3_pci_fault, SIGBUS, 0, "external abort on linefetch");
  595. hook_fault_code(6, v3_pci_fault, SIGBUS, 0, "external abort on linefetch");
  596. hook_fault_code(8, v3_pci_fault, SIGBUS, 0, "external abort on non-linefetch");
  597. hook_fault_code(10, v3_pci_fault, SIGBUS, 0, "external abort on non-linefetch");
  598. raw_spin_lock_irqsave(&v3_lock, flags);
  599. /*
  600. * Unlock V3 registers, but only if they were previously locked.
  601. */
  602. if (v3_readw(V3_SYSTEM) & V3_SYSTEM_M_LOCK)
  603. v3_writew(V3_SYSTEM, 0xa05f);
  604. /*
  605. * Setup window 0 - PCI non-prefetchable memory
  606. * Local: 0x40000000 Bus: 0x00000000 Size: 256MB
  607. */
  608. v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(non_mem.start) |
  609. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
  610. v3_writew(V3_LB_MAP0, v3_addr_to_lb_map(non_mem_pci) |
  611. V3_LB_MAP_TYPE_MEM);
  612. /*
  613. * Setup window 1 - PCI prefetchable memory
  614. * Local: 0x50000000 Bus: 0x10000000 Size: 256MB
  615. */
  616. v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(pre_mem.start) |
  617. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
  618. V3_LB_BASE_ENABLE);
  619. v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(pre_mem_pci) |
  620. V3_LB_MAP_TYPE_MEM_MULTIPLE);
  621. /*
  622. * Setup window 2 - PCI IO
  623. */
  624. v3_writel(V3_LB_BASE2, v3_addr_to_lb_base2(io_mem.start) |
  625. V3_LB_BASE_ENABLE);
  626. v3_writew(V3_LB_MAP2, v3_addr_to_lb_map2(0));
  627. /*
  628. * Disable PCI to host IO cycles
  629. */
  630. temp = v3_readw(V3_PCI_CFG) & ~V3_PCI_CFG_M_I2O_EN;
  631. temp |= V3_PCI_CFG_M_IO_REG_DIS | V3_PCI_CFG_M_IO_DIS;
  632. v3_writew(V3_PCI_CFG, temp);
  633. printk(KERN_DEBUG "FIFO_CFG: %04x FIFO_PRIO: %04x\n",
  634. v3_readw(V3_FIFO_CFG), v3_readw(V3_FIFO_PRIORITY));
  635. /*
  636. * Set the V3 FIFO such that writes have higher priority than
  637. * reads, and local bus write causes local bus read fifo flush.
  638. * Same for PCI.
  639. */
  640. v3_writew(V3_FIFO_PRIORITY, 0x0a0a);
  641. /*
  642. * Re-lock the system register.
  643. */
  644. temp = v3_readw(V3_SYSTEM) | V3_SYSTEM_M_LOCK;
  645. v3_writew(V3_SYSTEM, temp);
  646. /*
  647. * Clear any error conditions, and enable write errors.
  648. */
  649. v3_writeb(V3_LB_ISTAT, 0);
  650. v3_writew(V3_LB_CFG, v3_readw(V3_LB_CFG) | (1 << 10));
  651. v3_writeb(V3_LB_IMASK, 0x28);
  652. __raw_writel(3, ap_syscon_base + INTEGRATOR_SC_PCIENABLE_OFFSET);
  653. raw_spin_unlock_irqrestore(&v3_lock, flags);
  654. }
  655. static void __init pci_v3_postinit(void)
  656. {
  657. unsigned int pci_cmd;
  658. pci_cmd = PCI_COMMAND_MEMORY |
  659. PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE;
  660. v3_writew(V3_PCI_CMD, pci_cmd);
  661. v3_writeb(V3_LB_ISTAT, ~0x40);
  662. v3_writeb(V3_LB_IMASK, 0x68);
  663. #if 0
  664. ret = request_irq(IRQ_AP_LBUSTIMEOUT, lb_timeout, 0, "bus timeout", NULL);
  665. if (ret)
  666. printk(KERN_ERR "PCI: unable to grab local bus timeout "
  667. "interrupt: %d\n", ret);
  668. #endif
  669. register_isa_ports(non_mem.start, io_mem.start, 0);
  670. }
  671. /*
  672. * A small note about bridges and interrupts. The DECchip 21050 (and
  673. * later) adheres to the PCI-PCI bridge specification. This says that
  674. * the interrupts on the other side of a bridge are swizzled in the
  675. * following manner:
  676. *
  677. * Dev Interrupt Interrupt
  678. * Pin on Pin on
  679. * Device Connector
  680. *
  681. * 4 A A
  682. * B B
  683. * C C
  684. * D D
  685. *
  686. * 5 A B
  687. * B C
  688. * C D
  689. * D A
  690. *
  691. * 6 A C
  692. * B D
  693. * C A
  694. * D B
  695. *
  696. * 7 A D
  697. * B A
  698. * C B
  699. * D C
  700. *
  701. * Where A = pin 1, B = pin 2 and so on and pin=0 = default = A.
  702. * Thus, each swizzle is ((pin-1) + (device#-4)) % 4
  703. */
  704. /*
  705. * This routine handles multiple bridges.
  706. */
  707. static u8 __init pci_v3_swizzle(struct pci_dev *dev, u8 *pinp)
  708. {
  709. if (*pinp == 0)
  710. *pinp = 1;
  711. return pci_common_swizzle(dev, pinp);
  712. }
  713. static int irq_tab[4] __initdata = {
  714. IRQ_AP_PCIINT0, IRQ_AP_PCIINT1, IRQ_AP_PCIINT2, IRQ_AP_PCIINT3
  715. };
  716. /*
  717. * map the specified device/slot/pin to an IRQ. This works out such
  718. * that slot 9 pin 1 is INT0, pin 2 is INT1, and slot 10 pin 1 is INT1.
  719. */
  720. static int __init pci_v3_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  721. {
  722. int intnr = ((slot - 9) + (pin - 1)) & 3;
  723. return irq_tab[intnr];
  724. }
  725. static struct hw_pci pci_v3 __initdata = {
  726. .swizzle = pci_v3_swizzle,
  727. .setup = pci_v3_setup,
  728. .nr_controllers = 1,
  729. .ops = &pci_v3_ops,
  730. .preinit = pci_v3_preinit,
  731. .postinit = pci_v3_postinit,
  732. };
  733. #ifdef CONFIG_OF
  734. static int __init pci_v3_map_irq_dt(const struct pci_dev *dev, u8 slot, u8 pin)
  735. {
  736. struct of_irq oirq;
  737. int ret;
  738. ret = of_irq_map_pci(dev, &oirq);
  739. if (ret) {
  740. dev_err(&dev->dev, "of_irq_map_pci() %d\n", ret);
  741. /* Proper return code 0 == NO_IRQ */
  742. return 0;
  743. }
  744. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  745. oirq.size);
  746. }
  747. static int __init pci_v3_dtprobe(struct platform_device *pdev,
  748. struct device_node *np)
  749. {
  750. struct of_pci_range_parser parser;
  751. struct of_pci_range range;
  752. struct resource *res;
  753. int irq, ret;
  754. if (of_pci_range_parser_init(&parser, np))
  755. return -EINVAL;
  756. /* Get base for bridge registers */
  757. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  758. if (!res) {
  759. dev_err(&pdev->dev, "unable to obtain PCIv3 base\n");
  760. return -ENODEV;
  761. }
  762. pci_v3_base = devm_ioremap(&pdev->dev, res->start,
  763. resource_size(res));
  764. if (!pci_v3_base) {
  765. dev_err(&pdev->dev, "unable to remap PCIv3 base\n");
  766. return -ENODEV;
  767. }
  768. /* Get and request error IRQ resource */
  769. irq = platform_get_irq(pdev, 0);
  770. if (irq <= 0) {
  771. dev_err(&pdev->dev, "unable to obtain PCIv3 error IRQ\n");
  772. return -ENODEV;
  773. }
  774. ret = devm_request_irq(&pdev->dev, irq, v3_irq, 0,
  775. "PCIv3 error", NULL);
  776. if (ret < 0) {
  777. dev_err(&pdev->dev, "unable to request PCIv3 error IRQ %d (%d)\n", irq, ret);
  778. return ret;
  779. }
  780. for_each_of_pci_range(&parser, &range) {
  781. if (!range.flags) {
  782. of_pci_range_to_resource(&range, np, &conf_mem);
  783. conf_mem.name = "PCIv3 config";
  784. }
  785. if (range.flags & IORESOURCE_IO) {
  786. of_pci_range_to_resource(&range, np, &io_mem);
  787. io_mem.name = "PCIv3 I/O";
  788. }
  789. if ((range.flags & IORESOURCE_MEM) &&
  790. !(range.flags & IORESOURCE_PREFETCH)) {
  791. non_mem_pci = range.pci_addr;
  792. non_mem_pci_sz = range.size;
  793. of_pci_range_to_resource(&range, np, &non_mem);
  794. non_mem.name = "PCIv3 non-prefetched mem";
  795. }
  796. if ((range.flags & IORESOURCE_MEM) &&
  797. (range.flags & IORESOURCE_PREFETCH)) {
  798. pre_mem_pci = range.pci_addr;
  799. pre_mem_pci_sz = range.size;
  800. of_pci_range_to_resource(&range, np, &pre_mem);
  801. pre_mem.name = "PCIv3 prefetched mem";
  802. }
  803. }
  804. if (!conf_mem.start || !io_mem.start ||
  805. !non_mem.start || !pre_mem.start) {
  806. dev_err(&pdev->dev, "missing ranges in device node\n");
  807. return -EINVAL;
  808. }
  809. pci_v3.map_irq = pci_v3_map_irq_dt;
  810. pci_common_init_dev(&pdev->dev, &pci_v3);
  811. return 0;
  812. }
  813. #else
  814. static inline int pci_v3_dtprobe(struct platform_device *pdev,
  815. struct device_node *np)
  816. {
  817. return -EINVAL;
  818. }
  819. #endif
  820. static int __init pci_v3_probe(struct platform_device *pdev)
  821. {
  822. struct device_node *np = pdev->dev.of_node;
  823. int ret;
  824. /* Remap the Integrator system controller */
  825. ap_syscon_base = ioremap(INTEGRATOR_SC_BASE, 0x100);
  826. if (!ap_syscon_base) {
  827. dev_err(&pdev->dev, "unable to remap the AP syscon for PCIv3\n");
  828. return -ENODEV;
  829. }
  830. /* Device tree probe path */
  831. if (np)
  832. return pci_v3_dtprobe(pdev, np);
  833. pci_v3_base = devm_ioremap(&pdev->dev, PHYS_PCI_V3_BASE, SZ_64K);
  834. if (!pci_v3_base) {
  835. dev_err(&pdev->dev, "unable to remap PCIv3 base\n");
  836. return -ENODEV;
  837. }
  838. ret = devm_request_irq(&pdev->dev, IRQ_AP_V3INT, v3_irq, 0, "V3", NULL);
  839. if (ret) {
  840. dev_err(&pdev->dev, "unable to grab PCI error interrupt: %d\n",
  841. ret);
  842. return -ENODEV;
  843. }
  844. conf_mem.name = "PCIv3 config";
  845. conf_mem.start = PHYS_PCI_CONFIG_BASE;
  846. conf_mem.end = PHYS_PCI_CONFIG_BASE + SZ_16M - 1;
  847. conf_mem.flags = IORESOURCE_MEM;
  848. io_mem.name = "PCIv3 I/O";
  849. io_mem.start = PHYS_PCI_IO_BASE;
  850. io_mem.end = PHYS_PCI_IO_BASE + SZ_16M - 1;
  851. io_mem.flags = IORESOURCE_MEM;
  852. non_mem_pci = 0x00000000;
  853. non_mem_pci_sz = SZ_256M;
  854. non_mem.name = "PCIv3 non-prefetched mem";
  855. non_mem.start = PHYS_PCI_MEM_BASE;
  856. non_mem.end = PHYS_PCI_MEM_BASE + SZ_256M - 1;
  857. non_mem.flags = IORESOURCE_MEM;
  858. pre_mem_pci = 0x10000000;
  859. pre_mem_pci_sz = SZ_256M;
  860. pre_mem.name = "PCIv3 prefetched mem";
  861. pre_mem.start = PHYS_PCI_PRE_BASE + SZ_256M;
  862. pre_mem.end = PHYS_PCI_PRE_BASE + SZ_256M - 1;
  863. pre_mem.flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  864. pci_v3.map_irq = pci_v3_map_irq;
  865. pci_common_init_dev(&pdev->dev, &pci_v3);
  866. return 0;
  867. }
  868. static const struct of_device_id pci_ids[] = {
  869. { .compatible = "v3,v360epc-pci", },
  870. {},
  871. };
  872. static struct platform_driver pci_v3_driver = {
  873. .driver = {
  874. .name = "pci-v3",
  875. .of_match_table = pci_ids,
  876. },
  877. };
  878. static int __init pci_v3_init(void)
  879. {
  880. return platform_driver_probe(&pci_v3_driver, pci_v3_probe);
  881. }
  882. subsys_initcall(pci_v3_init);
  883. /*
  884. * Static mappings for the PCIv3 bridge
  885. *
  886. * e8000000 40000000 PCI memory PHYS_PCI_MEM_BASE (max 512M)
  887. * ec000000 61000000 PCI config space PHYS_PCI_CONFIG_BASE (max 16M)
  888. * fee00000 60000000 PCI IO PHYS_PCI_IO_BASE (max 16M)
  889. */
  890. static struct map_desc pci_v3_io_desc[] __initdata __maybe_unused = {
  891. {
  892. .virtual = (unsigned long)PCI_MEMORY_VADDR,
  893. .pfn = __phys_to_pfn(PHYS_PCI_MEM_BASE),
  894. .length = SZ_16M,
  895. .type = MT_DEVICE
  896. }, {
  897. .virtual = (unsigned long)PCI_CONFIG_VADDR,
  898. .pfn = __phys_to_pfn(PHYS_PCI_CONFIG_BASE),
  899. .length = SZ_16M,
  900. .type = MT_DEVICE
  901. }
  902. };
  903. int __init pci_v3_early_init(void)
  904. {
  905. iotable_init(pci_v3_io_desc, ARRAY_SIZE(pci_v3_io_desc));
  906. vga_base = (unsigned long)PCI_MEMORY_VADDR;
  907. pci_map_io_early(__phys_to_pfn(PHYS_PCI_IO_BASE));
  908. return 0;
  909. }